Django Model._meta API

Source: Internet
Author: User

The Model._meta API is the core of the Django Orm, which allows lookups, queries, forms, and admin modules to understand each model by using the _meta properties of each model class.

1. field Access API, retrieving a model's field instance using the name

Options.get_field (Field_name)

Returns a field instance based on the given field_name. Field_name can be a field in the model, an abstract or inherited model field, or a field on another model of a model, in which case the field_name is a user-defined Related_ Name or Django Auto-generated names.

Note: Hidden fields cannot be retrieved by name. If the given field name is not found, a fielddoesnotexist error is thrown.

  1. >>> from django.contrib.auth.models import User
  2. # A Field on the model
  3. >>> User._meta.get_field (' username ')
  4. <django.db.models.fields.CharField:username>
  5. # A field from another model that have A relation with the current model
  6. >>> User._meta.get_field (' logentry ')
  7. <ManyToOneRel:admin.logentry>
  8. # A Non existent field
  9. >>> User._meta.get_field (' does_not_exist ')
  10. Traceback (most recent):
  11. ...
  12. Fielddoesnotexist:user has no field named ' Does_not_exist '

2. Retrieving all field instances of a model
Options.get_fields (Include_parents=true, Include_hidden=false)

Returns all related fields of a model in the form of a tuple. Get_fields receives two parameters to control which fields are returned.

  1. >>> from django.contrib.auth.models import User
  2. >>> user._meta.get_fields ()
  3. (<manytoonerel:admin.logentry>,
  4. <django.db.models.fields.autofield:id>,
  5. <django.db.models.fields.charfield:password>,
  6. <django.db.models.fields.datetimefield:last_login>,
  7. <django.db.models.fields.booleanfield:is_superuser>,
  8. <django.db.models.fields.charfield:username>,
  9. <django.db.models.fields.charfield:first_name>,
  10. <django.db.models.fields.charfield:last_name>,
  11. <django.db.models.fields.emailfield:email>,
  12. <django.db.models.fields.booleanfield:is_staff>,
  13. <django.db.models.fields.booleanfield:is_active>,
  14. <django.db.models.fields.datetimefield:date_joined>,
  15. <django.db.models.fields.related.manytomanyfield:groups>,
  16. <django.db.models.fields.related.ManyToManyField:user_permissions>)

Original link: https://docs.djangoproject.com/en/2.0/ref/models/meta/

Django Model._meta API

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.