[Fixed]-How do Django model fields work?

5đź‘Ť

âś…

Yes, first_name and last_name are class variables. They define fields that will be created in a database table. There is a Person table that has first_name and last_name columns, so it makes sense for them to be at Class level at this point.

For more on models, see:
http://docs.djangoproject.com/en/dev/topics/db/models/

When it comes to accessing instances of a Person in code, you are typically doing this via Django’s ORM, and at this point they essentially behave as instance variables.

For more on model instances, see:
http://docs.djangoproject.com/en/dev/ref/models/instances/?from=olddocs

👤Andy Hume

24đź‘Ť

The essence of your question is “how come these class variables (which I assign Field objects to) suddenly become instance variables (which I assign data to) in Django’s ORM”? The answer to that is the magic of Python metaclasses.

A metaclass allows you to hook into and modify the process of creating a Python class (not the creation of an instance of that class, the creation of the class itself).

Django’s Model object (and thus also your models, which are subclasses) has a ModelBase metaclass. It looks through all the class attributes of your model, and any that are instances of a Field subclass it moves into a fields list. That list is assigned as an attribute of the _meta object, which is a class attribute of the model. Thus you can always get to the actual Field objects via MyModel._meta.fields, or MyModel._meta.get_field('field_name').

The Model.__init__ method is then able to use the _meta.fields list to determine what instance attributes should be initialized when a model instance is created.

Don’t be afraid to dive into the Django source code; it’s a great source of education!

👤Carl Meyer

1đź‘Ť

Not a real answer, but for enrichment:

Person.first_name 

won’t work

p = Person.objects.get(pk=x)
p.first_name

will work. so an object instance of person has a first and last name, but static context Person does not.

Also note: Django has Model Managers which are allow “Person” to do static queryset operations. (https://docs.djangoproject.com/en/dev/topics/db/managers/#managers).

so for example

peoples = Person.objects.all()

Leave a comment