Changes between Version 4 and Version 5 of DevModelCreation


Ignore:
Timestamp:
Apr 14, 2008, 5:45:32 AM (17 years ago)
Author:
Ivan Illarionov
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DevModelCreation

    v4 v5  
    3535Now things start to get interesting. A new class object with the ''Example'' name is created in the right module namespace. A {{{_meta}}} attribute is added to hold all of the field validation, retrieval and saving machinery. This is an {{{Options}}} class from {{{options.py}}}. Initially, it takes values from the inner {{{Meta}}} class, if any, on the new model, using defaults if {{{Meta}}} is not declared (as in our example).
    3636
    37 Each attribute is then added to the new class object. This is done by calling {{{Model.add_to_class}}} with the attribute name and object. This object could be something like a normal unbound method, or a string, or a property, or -- of most relevance here -- a {{{Field}}} subclass that we want to do something with. The {{{add_to_class()}}} method checks to see if the object has a {{{contribute_to_class}}} method that can be called. If it doesn't, this is just a normal attribute and it is added to the new class object with the given name. If we do have a {{{contribute_to_class}}} method on the new attribute object, this method is called and given a reference to the new class along with the name of the new attribute.
     37Each attribute is then added to the new class object. This is done by calling {{{ModelBase.add_to_class}}} with the attribute name and object. This object could be something like a normal unbound method, or a string, or a property, or -- of most relevance here -- a {{{Field}}} subclass that we want to do something with. The {{{add_to_class()}}} method checks to see if the object has a {{{contribute_to_class}}} method that can be called. If it doesn't, this is just a normal attribute and it is added to the new class object with the given name. If we do have a {{{contribute_to_class}}} method on the new attribute object, this method is called and given a reference to the new class along with the name of the new attribute.
    3838
    3939Putting this in the context of our example, the ''static'' and ''!__unicode!__'' attributes would be added normally to the class as a string object and unbound method, respectively. When the ''name'' attribute is considered, {{{add_to_class}}} would be called with the string ''name'' and an instance of the {{{models.CharField}}} class (which is defined in {{{fields/__init__.py}}}). Note that we are passed an ''instance'' of {{{CharField}}}, not the class itself. The object we are passed in {{{add_to_class}}} has already been created. So that object knows it has a ''maxlength'' of 50 in our case. And that the default verbose name is not being overridden and so on. {{{CharField}}} instances have a {{{contribute_to_class}}} method, so that will be called and passed the new {{{Example}}} class object and the string ''name'' (the attribute name that we are creating).
Back to Top