310👍
You are asking a question about two different things:
-
Meta
inner class in Django models:This is just a class container with some options (metadata) attached to the model. It defines such things as available permissions, associated database table name, whether the model is abstract or not, singular and plural versions of the name etc.
Short explanation is here: Django docs: Models: Meta options
List of available meta options is here: Django docs: Model Meta options
For latest version of Django: Django docs: Model Meta options
-
Metaclass in Python:
The best description is here: What are metaclasses in Python?
69👍
Extending on Tadeck’s Django answer above, the use of ‘class Meta:’ in Django is just normal Python too.
The internal class is a convenient namespace for shared data among the class instances (hence the name Meta for ‘metadata’ but you can call it anything you like). While in Django it’s generally read-only configuration stuff, there is nothing to stop you changing it:
In [1]: class Foo(object):
...: class Meta:
...: metaVal = 1
...:
In [2]: f1 = Foo()
In [3]: f2 = Foo()
In [4]: f1.Meta.metaVal
Out[4]: 1
In [5]: f2.Meta.metaVal = 2
In [6]: f1.Meta.metaVal
Out[6]: 2
In [7]: Foo.Meta.metaVal
Out[7]: 2
You can explore it in Django directly too e.g:
In [1]: from django.contrib.auth.models import User
In [2]: User.Meta
Out[2]: django.contrib.auth.models.Meta
In [3]: User.Meta.__dict__
Out[3]:
{'__doc__': None,
'__module__': 'django.contrib.auth.models',
'abstract': False,
'verbose_name': <django.utils.functional.__proxy__ at 0x26a6610>,
'verbose_name_plural': <django.utils.functional.__proxy__ at 0x26a6650>}
However, in Django you are more likely to want to explore the _meta
attribute which is an Options
object created by the model metaclass
when a model is created. That is where you’ll find all of the Django class ‘meta’ information. In Django, Meta
is just used to pass information into the process of creating the _meta
Options
object.
- [Django]-Uninstall Django completely
- [Django]-How to get username from Django Rest Framework JWT token
- [Django]-Django related_name for field clashes
29👍
Django’s Model
class specifically handles having an attribute named Meta
which is a class. It’s not a general Python thing.
Python metaclasses are completely different.
- [Django]-Get the list of checkbox post in django views
- [Django]-Cannot access django app through ip address while accessing it through localhost
- [Django]-Django + Ajax
16👍
Answers that claim Django model’s Meta
and metaclasses are "completely different" are misleading answers.
The construction of Django model class objects, that is to say the object that stands for the class definition itself (yes, classes are also objects), are indeed controlled by a metaclass called ModelBase
, and you can see that code here.
And one of the things that ModelBase
does is to create the _meta
attribute on every Django model which contains validation machinery, field details, save logic and so forth. During this operation, the stuff that is specified in the model’s inner Meta
class is read and used within that process.
So, while yes, in a sense Meta
and metaclasses are different ‘things’, within the mechanics of Django model construction they are intimately related; understanding how they work together will deepen your insight into both at once.
This might be a helpful source of information to better understand how Django models employ metaclasses.
https://code.djangoproject.com/wiki/DevModelCreation
And this might help too if you want to better understand how objects work in general.
- [Django]-OneToOneField() vs ForeignKey() in Django
- [Django]-What's the difference between CharField and TextField in Django?
- [Django]-Creating a JSON response using Django and Python
5👍
Inner Meta Class Document:
This document of django
Model metadata is “anything that’s not a field”, such as ordering options (ordering), database table name (db_table), or human-readable singular and plural names (verbose_name and verbose_name_plural)
. None are required, and adding class Meta to a model is completely optional.
https://docs.djangoproject.com/en/dev/topics/db/models/#meta-options
- [Django]-How to use pdb.set_trace() in a Django unittest?
- [Django]-Data Mining in a Django/Postgres application
- [Django]-How to get the current URL within a Django template?
1👍
In Django, it acts as a configuration class and keeps the configuration data in one place!!
- [Django]-Django: list all reverse relations of a model
- [Django]-Django: Filter a Queryset made of unions not working
- [Django]-Django TextField and CharField is stripping spaces and blank lines
1👍
Class Meta is the place in your code logic where your model.fields MEET With your form.widgets.
So under Class Meta() you create the link between your model’ fields and the different widgets you want to have in your form.
- [Django]-Using JSON in django template
- [Django]-No handlers could be found for logger
- [Django]-What does 'many = True' do in Django Rest FrameWork?