3

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']
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Ali_pishahang
  • 33
  • 1
  • 4
  • Meta classes are a python feature, not a django feature but django uses them. This will help: https://realpython.com/python-metaclasses/ – Dov Rine Jul 28 '19 at 14:04
  • 1
    @DovRine: this is *not* a `metaclass`, since metaclasses are implemented through the inheritance mechanism (that would be `Author(models.Model, metaclass=...)`. – Willem Van Onsem Jul 28 '19 at 14:05
  • @WillemVanOnsem: You're right. I found this that helps me. Maybe you too: https://stackoverflow.com/questions/10344197/how-does-djangos-meta-class-work – Dov Rine Jul 28 '19 at 14:07
  • it seems you haven't checked the [documentation](https://docs.djangoproject.com/en/2.2/topics/db/models/#meta-options). 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). – Robin Zigmond Jul 28 '19 at 14:09

2 Answers2

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.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
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

Hisham___Pak
  • 1,472
  • 1
  • 11
  • 23