_meta property Django's model

  Python reflective mechanism, Django is no exception, but also a good reflection, each model has a property Django _meta, _meta have attributes and methods, these properties and methods which some reflection characteristic model, if with _meta the good, the code is not only more beautiful, but also greatly enhance the versatility and reusability of code. The following describes _meta properties and methods.

  In django project, to define a model, followed by dir () function to print out _meta properties and methods of this model, the following results:

_meta Properties and Methods

'__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_field_cache', '_field_name_cache', '_fields', '_fill_fields_cache', '_fill_m2m_cache', '_fill_related_many_to_many_cache', '_fill_related_objects_cache', '_join_cache', '_m2m_cache', '_many_to_many', '_name_map', '_prepare', '_related_many_to_many_cache', '_related_objects_cache', '_related_objects_proxy_cache', 'abstract', 'abstract_managers', 'add_field', 'add_virtual_field', 'admin', 'app_label', 'auto_created', 'auto_field', 'concrete_managers', 'concrete_model', 'contribute_to_class', 'db_table', 'db_tablespace', 'duplicate_targets', 'fields', 'get_add_permission', 'get_all_field_names', 'get_all_related_m2m_objects_with_model', 'get_all_related_many_to_many_objects', 'get_all_related_objects', 'get_all_related_objects_with_model', 'get_ancestor_link', 'get_base_chain', 'get_change_permission', 'get_delete_permission', 'get_field', 'get_field_by_name', 'get_fields_with_model', 'get_latest_by', 'get_m2m_with_model', 'get_ordered_objects', 'get_parent_list', 'has_auto_field', 'init_name_map', 'installed', 'local_fields', 'local_many_to_many', 'managed', 'many_to_many', 'module_name', 'object_name', 'order_with_respect_to', 'ordering', 'parents', 'permissions', 'pk', 'pk_index', 'proxy', 'proxy_for_model', 'related_fkey_lookups', 'setup_pk', 'setup_proxy', 'unique_together', 'verbose_name', 'verbose_name_plural', 'verbose_name_raw', 'virtual_fields'

 

Here are some of its main properties and methods.

_field_cache: field type of cache is a tuple, which reflect the elements of each field of the model type, returns the result in the form of the following:

((<django.db.models.fields.AutoField: id>, None), (<django.db.models.fields.DateTimeField: create_time>, None), (<django.db.models.fields.related.ForeignKey: create_user>, None), (<django.db.models.fields.DateTimeField: write_time>, None), (<django.db.models.fields.related.ForeignKey: write_user>, None), (<django.db.models.fields.related.ForeignKey: confirm_user>, None), (<django.db.models.fields.DateTimeField: confirm_date>, None))

_field_name_cache: with the above _field_cache similar results returned the following form:

[<django.db.models.fields.AutoField: id>, <django.db.models.fields.DateTimeField: create_time>, <django.db.models.fields.related.ForeignKey: create_user>, <django.db.models.fields.DateTimeField: write_time>, <django.db.models.fields.related.ForeignKey: write_user>, <django.db.models.fields.related.ForeignKey: confirm_user>]

abstract: Boolean, indicating whether or not an abstract class, an abstract class is not instantiated. Since python is not an abstract class, interface concept, so to achieve this function to obtain this module abc

abstract_managers: Returns the abstract Manager list manager is Django's model database query interface. Each model Django application has at least one manager

add_field (): in order to insert a field, the function prototype add_field (field, private = False, virtual = NOT_PROVIDED), file type parameter is a field instance. Source may participate in this part of the links below https://github.com/django/django/blob/master/django/db/models/base.py

add_virtual_field (): add a virtual field, the function prototype add_virtual_field (field, varargs = None, keywords = None, defaults = None), of the available _meta virtual_fields properties can view all fields under the virtual model,

app_label : name of the property, the model where the app package

auto_created: Boolean value that indicates whether to automatically create

auto_field: Properties, returns all fields of the self-energizing type field, generally `id` field, such as <django.db.models.fields.AutoField: id>

concrete_managers: Returns the specific list manager, default, Django model for each class of objects called add a manager, so by default, meaning that the value of at least one manager objects. If you have a custom manager, then this custom manager can get through this property.

concrete_model: property, returned to the model itself, by the attribute value can be used to operate its access attributes of a field, including field names, etc. may be empty if '.'

contribute_to_class (): I do not know what role, source https://github.com/django/django/blob/master/django/db/models/options.py

db_table : property, the data used in the model name of the table, about the name of the data table, you can see django documentation, http: //python.usyiyi.cn/django/ref/models/options.html

db_tablespace: the name of the current model used by database table space. The default value is set in the project DEFAULT_TABLESPACE, if it exists

duplicate_targets : property, the return value is the dictionary indicating the model name of the field but not the same as the attribute of fields, such as the following example:

from django.db import models
from django.contrib.auth.models import User
class A(models.Model):
    create_time = models.DateTimeField(auto_now_add=True)
    create_user = models.ForeignKey(User,related_name='%(app_label)s_%(class)s_create_user')
    write_time = models.DateTimeField(auto_now=True,blank=True,null=True)
    write_user = models.ForeignKey(User,related_name='%(app_label)s_%(class)s_write_user',blank=True)
    confirm_user = models.ForeignKey(User,blank=True,null=True,)
    confirm_date = models.DateTimeField(blank=True,null=True)
    owner = models.ForeignKey(User,related_name='purchases_owner')
    review = models.ForeignKey(User,blank=True, null=True,related_name='review')

那么A._meta.duplicate_targets的值就为{'create_user_id': set(['review_id', 'write_user_id', 'owner_id']), 'review_id': set(['create_user_id', 'write_user_id', 'owner_id']), 'write_user_id': set(['create_user_id', 'review_id', 'owner_id']), 'owner_id': set(['create_user_id', 'review_id', 'write_user_id'])}

They are associated with a class User.

fields属性,返回一个列表,列出了该模型的所有字段,如下[<django.db.models.fields.AutoField: id>, <django.db.models.fields.DateTimeField: create_time>, <django.db.models.fields.related.ForeignKey: create_user>, <django.db.models.fields.DateTimeField: write_time>, <django.db.models.fields.related.ForeignKey: write_user>, <django.db.models.fields.related.ForeignKey: parent>, <django.db.models.fields.CharField: state>, <django.db.models.fields.related.ForeignKey: owner>, <django.db.models.fields.related.ForeignKey: review>, <django.db.models.fields.CharField: name>, <django.db.models.fields.CharField: tags>,]

We know that the field is actually a Field object, its properties can reflect the characteristics of the field, which is the model field options, such as verbose_name, name, primary_key, max_length, unique, blank, and so on (see Field class source code https: // github. com / django / django / blob / master / django / db / models / fields / __ init__.py, meaning the model field options see http://python.usyiyi.cn/django/ref/models/fields.html)

get_add_permission (): Gets the "Add" permission, returns a string, there are similar get_change_permission () , get_delete_permission ()

get_all_field_names (): returns a list, a list of all the elements of the model field names, how to understand this all too? This model not only includes all the fields of their own definition, the model also includes other fields associated with it (i.e. ForeignKey, ManyToManyField, OneToOneField)

get_all_related_m2m_objects_with_model (): Get all models exist ManyToManyField relationship with the model returns a list, for example, [(<RelatedObject: human: humansupplier related to user>, None), (<RelatedObject: human: humanrole related to user>, None) ]

get_all_related_many_to_many_objects (): with the above method, only in a slightly different form of the return value of [<RelatedObject: human: humansupplier related to user>, <RelatedObject: human: humanrole related to user>], there are similar get_all_related_objects () , get_all_related_objects_with_model () , all models get an association relationship with the model

get_field (): Prototype get_field (name, many_to_many = True) , field_name string, the return Field object model field corresponding to the field name, such as <django.db.models.fields.related.ForeignKey: create_user>, if the field name does not exist, it returns an abnormal FieldDoesNotExist

get_field_by_name (): ibid., the return value richer than those above (<django.db.models.fields.related.ForeignKey: create_user>, None , True, False), respectively (field_object, model, direct, m2m ), if there is direct model of this field is True, if the model exists ManyToManyField relationship, m2m is True

get_fields_with_model (): Returns the model of all fields (field model) sequence, the model for the field, the element on the current model is no

get_latest_by: property, returns the name of a sortable field model, if you are in the meta model option defines the values of get_latest_by, then _meta.get_latest_by returns the value of the yuan get_latest_by options defined, otherwise it returns None, About yuan options , see django documentation http://python.usyiyi.cn/django/ref/models/options.html

get_m2m_with_model():是get_fields_with_model()的many-to-many版本

get_ordered_objects () : Returns the option to sort objects according to the object list

has_auto_field: property, returns a Boolean value that indicates that the model has no increment field

init_name_map (): initializing field name to field object (i.e. Object Field) mapping,

installed: property, Boolean value, the model where the app is in the setting file django's configuration, that INSTALLED_APPS setting file whether there is the model name of the app where

local_fields: Properties, returns all fields of the local model, the return value is a list of elements is a field type

local_many_to_many: property, return all of the model is the relationship between the fields many_to_many, excluding the parent class, the return value is a list; many_to_many there is a similar model and return parent class is a list of all fields many_to_many relationship.

managed: property, Boolean

MODEL_NAME : property, return to the model name is lowercase, type a string

object_name : property, model name, string, but not lowercase, with the definition of what is the name of the model shown here is what is the name, is the prototype

ordering: property, it returns a list of objects default order. If you define the ordering of values in the meta-model options, then _meta.ordering ordering will return the value of yuan options defined, otherwise []

permissions: property, returns a list, returns additional permissions permissions table when creating an object if you define the value of the dollar options permissions model, you _meta.permissions returns the value of yuan options permissions defined otherwise. return [], similar to this mechanism there unique_together, verbose_name, verbose_name_plural, verbose_name_raw

pk: properties, return to the main key field type

pk_index (): method returns the index of the primary key field in the fields list.

 

 

*** The above content is not complete, according to the actual situation to find ***

Guess you like

Origin www.cnblogs.com/shuai1991/p/10960684.html