The Django model is how to add fields to the meta

Source: Internet
Author: User

def Contribute_to_class (self, CLS, name):        self.set_attributes_from_name (name)        = cls        cls._ Meta.add_field (self)        if  self.choices:            'get_%s_display  ' % self.name,                    Curry (Cls._get_field_display, FIELD=self)

The Contribute_to_class (self, CLS, name) defined above in the field class calls _meta.add_field (self)

defAdd_field (Self, field):#Insert the given field in the order in which it is created, using        #the "Creation_counter" attribute of the field.        #Move Many-to-many related fields from Self.fields into        #Self.many_to_many.        ifField.rel andisinstance (Field.rel, Manytomanyrel): Self.local_many_to_many.insert (Bisect (Self.local_many_to_many, field ), field)ifHasattr (Self,'_m2m_cache'):                delSelf._m2m_cacheElse: Self.local_fields.insert (bisect (self.local_fields, field), field) self.setup_pk (field) ifHasattr (Self,'_field_cache'):                delSelf._field_cachedelSelf._field_name_cacheifHasattr (Self,'_name_map'):            delSelf._name_map

Add_field will add field to the Meta object.

Fields =Property (_fields)defGet_fields_with_model (self):"""Returns a sequence of (field, model) pairs for all fields. The "model" element is None of the for fields on the current model.        Mostly of Use if constructing queries so, we know which model a field belongs to. """        Try: Self._field_cacheexceptAttributeerror:self._fill_fields_cache ()returnSelf._field_cachedef_fill_fields_cache (self): Cache= []         forParentinchself.parents: forfield, modelinchParent._meta.get_fields_with_model ():ifmodel:cache.append ((field, model))Else: Cache.append ((field, parent)) Cache.extend ([(F, None) forFinchSelf.local_fields]) Self._field_cache=tuple (cache) Self._field_name_cache= [x forX, _inchCache

Fields that call meta will return all field fields of model

Model instantiation to get all the field

The Django model is how to add fields to the meta

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.