Model _meta API

    • class [源代码]
    • The model _meta API is at the core of the Django ORM. It enables otherparts of the system such as lookups, queries, forms, and the admin tounderstand the capabilities of each model. The API is accessible throughthe _meta attribute of each model class, which is an instance of andjango.db.models.options.Options object.
    • Retrieve all field instances of a model
    • Retrieve a single field instance of a model by name
    • Options.getfield(_field_name)[源代码]
    • Returns the field instance given a name of a field.

    Retrieving all field instances of a model

    • Options.getfields(_include_parents=True, include_hidden=False)
      • include_parents
      • by default. Recursively includes fields defined on parentclasses. If set to False, get_fields() will only search forfields declared directly on the current model. Fields from models thatdirectly inherit from abstract models or proxy classes are consideredto be local, not on the parent.
      • include_hidden
      • False by default. If set to True, get_fields() will includefields that are used to back other field's functionality. This willalso include any fields that have a related_name (suchas , orForeignKey) that start with a "+".
    1. >>> from django.contrib.auth.models import User
    2. (<ManyToOneRel: admin.logentry>,
    3. <django.db.models.fields.AutoField: id>,
    4. <django.db.models.fields.CharField: password>,
    5. <django.db.models.fields.DateTimeField: last_login>,
    6. <django.db.models.fields.BooleanField: is_superuser>,
    7. <django.db.models.fields.CharField: username>,
    8. <django.db.models.fields.CharField: first_name>,
    9. <django.db.models.fields.CharField: last_name>,
    10. <django.db.models.fields.EmailField: email>,
    11. <django.db.models.fields.BooleanField: is_staff>,
    12. <django.db.models.fields.BooleanField: is_active>,
    13. <django.db.models.fields.DateTimeField: date_joined>,
    14. <django.db.models.fields.related.ManyToManyField: groups>,
    15. <django.db.models.fields.related.ManyToManyField: user_permissions>)
    16.  
    17. # Also include hidden fields.
    18. (<ManyToOneRel: auth.user_groups>,
    19. <ManyToOneRel: auth.user_user_permissions>,
    20. <ManyToOneRel: admin.logentry>,
    21. <django.db.models.fields.AutoField: id>,
    22. <django.db.models.fields.CharField: password>,
    23. <django.db.models.fields.DateTimeField: last_login>,
    24. <django.db.models.fields.BooleanField: is_superuser>,
    25. <django.db.models.fields.CharField: username>,
    26. <django.db.models.fields.CharField: first_name>,
    27. <django.db.models.fields.CharField: last_name>,
    28. <django.db.models.fields.EmailField: email>,
    29. <django.db.models.fields.BooleanField: is_staff>,
    30. <django.db.models.fields.BooleanField: is_active>,
    31. <django.db.models.fields.DateTimeField: date_joined>,
    32. <django.db.models.fields.related.ManyToManyField: groups>,
    33. <django.db.models.fields.related.ManyToManyField: user_permissions>)