Search code examples
pythonsqldjangomodels

Values from models from Database (Django)


I have this model

class Type(models.Model):
    type = models.CharField(max_length=50)
    value = models.CharField(max_length=1)

And into it, I have some data from an sql file:

INSERT INTO quest_type (type, value) VALUES ('Noun', '1');
INSERT INTO quest_type (type, value) VALUES ('Adjective', '2');
INSERT INTO quest_type (type, value) VALUES ('Duration', '3');

How do I access these values in the python shell? For example, if I know the type, how do I get the value (and vice verse)? I'm not sure how the syntax works.


Solution

  • you should be able to get that with

    Type.objects.filter(type=typeImInterestedIn)

    A couple of things to be leary of: -you probably want to avoid manually writing to a DB that you're using an ORM in. It just creates potential for mismatches. -naming an object Type is little problematic since it's so close to the python native function type.