Mionsamhail _meta APILink to this heading

class OptionsLink to this definition

Tá an tsamhail _meta API ag croílár an ORM Django. Cuireann sé ar chumas codanna eile den chóras mar chuardaigh, ceisteanna, foirmeacha agus an riarachán cumais gach samhail a thuiscint. Tá an API inrochtana trí thréith _meta gach aicme samhail, ar shampla é de réad Django.db.Models.Options.Options.

Methods and attributes that it provides can be used to:

  • Aisghabháil gach cás réimse de mhúnla

  • Aisghabháil sampla réimse amháin de mhúnla de réir ainm

  • Retrieve all fields that compose the primary key of a model

API rochtana allamuighLink to this heading

Sampla réimse amháin de mhúnla a aisghabháil de réir ainmLink to this heading

Options.get_field(field_name)Link to this definition

Tugann ainm réimse ar ais.

Is féidir le field_name` a bheith ina ainm réimse ar an tsamhail, réimse ar mhúnla teibí nó oidhreachta, nó réimse atá sainmhínithe ar mhúnla eile a thugann in iúl don tsamhail. Sa chás deireanach sin, is é an field_name` (in ord rogha) an:attr: ~.foreignkey.related_query_name a shocraíonn an t-úsáideoir, an:attr: ~.foreignKey.related_name atá leagtha amach ag an úsáideoir, nó an t-ainm a ghineann Django go huathoibríoch.

Hidden fields, fields with hidden=True, cannot be retrieved by name.

Mura bhfaightear réimse leis an ainm a thugtar a:class: ~django.core.exceptions.fieldDoesNotExist ardófar eisceacht.

Python console
>>> from django.contrib.auth.models import User

# A field on the model
>>> User._meta.get_field("username")
<django.db.models.fields.CharField: username>

# A field from another model that has a relation with the current model
>>> User._meta.get_field("logentry")
<ManyToOneRel: admin.logentry>

# A non existent field
>>> User._meta.get_field("does_not_exist")
Traceback (most recent call last):
    ...
FieldDoesNotExist: User has no field named 'does_not_exist'

Aisghabháil gach cás allamuigh de mhúnlaLink to this heading

Options.get_fields(include_parents=True, include_hidden=False)Link to this definition

Tugann sé cúpla réimsí a bhaineann le samhail ar ais. Glacann ``get_fields () ``le dhá pharaiméadar is féidir a úsáid chun na réimsí a chuirtear ar ais a rialú:

``include_tuismitheoirí ``

True de réir réamhshocraithe. Cuimsíonn sé go réimsí atá sainithe ar ranganna tuismitheoirí Má tá sé socraithe ar False, ní chuardóidh ``get_fields () ``ach réimsí a dearbhaíodh go díreach ar an tsamhail reatha. Meastar go bhfuil réimsí ó mhúnlaí a dhéanann oidhreacht go díreach ó mhúnlaí teibí nó ó ranganna seachfhreastalaí áitiúla, ní ar an

include_hidden

False by default. If set to True, get_fields() will include fields with hidden=True.

Python console
>>> from django.contrib.auth.models import User
>>> User._meta.get_fields()
(<ManyToOneRel: admin.logentry>,
 <django.db.models.fields.AutoField: id>,
 <django.db.models.fields.CharField: password>,
 <django.db.models.fields.DateTimeField: last_login>,
 <django.db.models.fields.BooleanField: is_superuser>,
 <django.db.models.fields.CharField: username>,
 <django.db.models.fields.CharField: first_name>,
 <django.db.models.fields.CharField: last_name>,
 <django.db.models.fields.EmailField: email>,
 <django.db.models.fields.BooleanField: is_staff>,
 <django.db.models.fields.BooleanField: is_active>,
 <django.db.models.fields.DateTimeField: date_joined>,
 <django.db.models.fields.related.ManyToManyField: groups>,
 <django.db.models.fields.related.ManyToManyField: user_permissions>)

# Also include hidden fields.
>>> User._meta.get_fields(include_hidden=True)
(<ManyToOneRel: auth.user_groups>,
 <ManyToOneRel: auth.user_user_permissions>,
 <ManyToOneRel: admin.logentry>,
 <django.db.models.fields.AutoField: id>,
 <django.db.models.fields.CharField: password>,
 <django.db.models.fields.DateTimeField: last_login>,
 <django.db.models.fields.BooleanField: is_superuser>,
 <django.db.models.fields.CharField: username>,
 <django.db.models.fields.CharField: first_name>,
 <django.db.models.fields.CharField: last_name>,
 <django.db.models.fields.EmailField: email>,
 <django.db.models.fields.BooleanField: is_staff>,
 <django.db.models.fields.BooleanField: is_active>,
 <django.db.models.fields.DateTimeField: date_joined>,
 <django.db.models.fields.related.ManyToManyField: groups>,
 <django.db.models.fields.related.ManyToManyField: user_permissions>)

Retrieving fields composing the primary key of a modelLink to this heading

Options.pk_fieldsLink to this definition

Returns a list of the fields composing the primary key of a model.

When a CompositePrimaryKey is defined on a model it will contain all the fields referenced by it.

Python
from django.db import models


class TenantUser(models.Model):
    pk = models.CompositePrimaryKey("tenant_id", "id")
    tenant_id = models.IntegerField()
    id = models.IntegerField()
Python console
>>> TenantUser._meta.pk_fields
[
    <django.db.models.fields.IntegerField: tenant_id>,
    <django.db.models.fields.IntegerField: id>
]

Otherwise it will contain the single field declared as the primary key of the model, either explicitly with primary_key=True or implicitly as the automatic primary key.

Python console
>>> User._meta.pk_fields
[<django.db.models.fields.AutoField: id>]