Django, Python, and Class Variables

I am learning Python at the same time when building Django. I am familiar with many other languages.

In the following code snippet, x is a class variable of class Foo .

 class Foo(object): x = 9000 

Given the previous announcement, the following works fine.

print Foo.x

The Django framework allows you to create your model by defining Python classes. It makes fields from different class variables in your Python classes.

 class Question(models.Model): question_text = models.CharField(max_length=200) 

Why the following code snippet:

 #!/usr/bin/env import os, django os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' django.setup() from polls.models import Question, Choice print Question.question_text 

enter the following error:

 AttributeError: type object 'Question' has no attribute 'question_text' 

As far as I understand everything, my Question class has one static member: Question.question_text .

+5
source share
3 answers

Magic.

No really.

Python classes are not a composite structure like in C ++. They themselves, just objects, are instances of another type:

 class Foo(object): pass print(type(Foo)) # <class 'type'> 

You can even make a class as if you made any other object by calling type . It:

 class Bar(object): a = 1 b = 2 

Actually (more or less) syntactic sugar for this:

 Bar = type('Bar', (object,), {'a': 1, 'b': 2}) 

type takes the name of your new class, a list of its superclasses and a specification of all the attributes of the class and splashes out the new class.

But since type is just a class like any other, you can subclass it and give it a different behavior. And this is what Django did: he created a subclass of type that does something else with the attribute attribute that you pass to it.

You do not see this happening directly in your own code, but if you check type(models.Model) , you will find out that its type is not type , but something special for Django. He probably has a β€œmeta” in the name because he called the metaclass: class of the class.

This is a fairly common template for creating "declarative" libraries in Python, where class attributes actually define some structure. You can see the same thing in form validation (wtforms), schema validation (colander), other ORMs (sqlalchemy), and even the stdlib enumeration module.

0
source

Django models use metaclass to change the behavior of a normal class.

Use dir(Question) , and now you will see that there are different attributes in this class. This is the usual behavior for Django models only.

If you're interested, you can learn the metaclass __new__ method , but it does a lot of work specific to the tasks of relational object matching,

+3
source

A question is an object of type type. You need a copy of the question:

 >>> q= Question(text = "Does a dog have the buddha nature?") 

Then you should get

q.text "Does the dog have the nature of a buddha?"

Note that this object will not be saved unless you save ():

 >>> q.save() 
+1
source

Source: https://habr.com/ru/post/1209031/


All Articles