Monday, June 28, 2021

Metaprogramming in Python

The term metaprogramming refers to the potential for a program to have knowledge of or manipulate itself. Python supports a form of metaprogramming for classes called metaclasses. Metaclasses are an esoteric OOP concept, lurking behind virtually all Python code. You are using them whether you are aware of it or not.


When the need arises, however, Python provides a capability that not all object-oriented languages support: you can get under the hood and define custom metaclasses


understanding Python metaclasses is worthwhile, because it leads to a better understanding of the internals of Python classes in general. You never know: you may one day find yourself in one of those situations where you just know that a custom metaclass is what you want.


Django Model_meta API 


The model _meta API is at the core of the Django ORM. It enables other parts of the system such as lookups, queries, forms, and the admin to understand the capabilities of each model. The API is accessible through the _meta attribute of each model class, which is an instance of an django.db.models.options.Options object.



Methods that it provides can be used to:

  • Retrieve all field instances of a model
  • Retrieve a single field instance of a model by name


Field access API


Retrieving a single field instance of a model by name


Options.get_field(field_name)

Returns the field instance given a name of a field.

field_name can be the name of a field on the model, a field on an abstract or inherited model, or a field defined on another model that points to the model. In the latter case, the field_name will be (in order of preference) the related_query_name set by the user, the related_name set by the user, or the name automatically generated by Django.


Hidden fields cannot be retrieved by name.


If a field with the given name is not found a FieldDoesNotExist exception will be raised.


Example is given below 


from django.contrib.auth.models import User

User._meta.get_field('username')


A field from another model that has a relation with the current model

User._meta.get_field('logentry')

<ManyToOneRel: admin.logentry>



User._meta.get_field('does_not_exist')

Traceback (most recent call last):

    ...

FieldDoesNotExist: User has no field named 'does_not_exist'


Retrieving all field instances of a model

Options.get_fields(include_parents=Trueinclude_hidden=False)



include_parents

True by default. Recursively includes fields defined on parent classes. If set to Falseget_fields() will only search for fields declared directly on the current model. Fields from models that directly inherit from abstract models or proxy classes are considered to be local, not on the parent.



include_hidden

False by default. If set to Trueget_fields() will include fields that are used to back other field’s functionality. This will also include any fields that have a related_name (such as ManyToManyField, or ForeignKey) that start with a “+”.



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>)






No comments:

Post a Comment