What is exactly Meta in Django?
Asked Answered
E

3

5

I want know simply what is Meta class in Django and what they do.

from django.db import models

Class Author(models.Model):
    first_name=models.CharField(max_length=20)
    last_name=models.CharField(max_length=20)
    
    class Meta:
        ordering=['last_name','first_name']
Elbrus answered 28/7, 2019 at 13:59 Comment(4)
Meta classes are a python feature, not a django feature but django uses them. This will help: realpython.com/python-metaclassesOlid
@DovRine: this is not a metaclass, since metaclasses are implemented through the inheritance mechanism (that would be Author(models.Model, metaclass=...).Scarper
@WillemVanOnsem: You're right. I found this that helps me. Maybe you too: #10344697Olid
it seems you haven't checked the documentation. They're just a way to specify some standard options about how your model should behave. They are nothing to do with Python metaclasses (although it's an understandable confusion and one I had when I was first learning Python and Django).Hydrothermal
S
6

Meta is a word that originates from the ancient Greeks and it means "meta is used to describe something that's self-reflective or self-referencing.". Specific to Django it is a class in which you describe certain aspects of your model. For example how the records should be ordered by default, what the name of the database table for that model is, etc.

The documentation on meta options [Django-doc] says:

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.

The Django documentation contains an exhaustive list of Django's model Meta options. For example for the ordering attribute [Django-doc]:

The default ordering for the object, for use when obtaining lists of objects. (...)

Here the ordering specifies that if you query for Author objects, like Author.objects.all(), then Django will, if you do not specify any ordering, order the Authors by last_name first, and in case of a tie, order by first_name.

Scarper answered 28/7, 2019 at 14:8 Comment(0)
M
1

You are asking a question about two different things:

  1. 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

Copied from here, consider liking: How does Django's Meta class work?

Read this for further understanding

Masked answered 28/7, 2019 at 14:11 Comment(0)
H
0

A Meta class in Django is a class that defines the behavior of other classes, such as models and forms. It is used to provide additional information about a class, such as its database table name, ordering, and other options. Meta classes are used to customize the behavior of Django models and forms.

For Django 5.0 you will find various class Meta options in the following link- Django official Documentation of Class Meta

Higgins answered 10/1, 2024 at 10:52 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.