Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of meta class in django

Can someone please explain why is meta class used in the following example.

Ex:

Class Employee (models.Model):
    name = models.ForeignKey(name)
    Gender = models.IntegerField()


    class Meta:
        ordering = ["Gender"]

Thanks.

like image 311
Hulk Avatar asked Jan 27 '10 11:01

Hulk


People also ask

Why we use meta classes in Python?

A metaclass in Python is a class of a class that defines how a class behaves. A class is itself an instance of a metaclass. A class in Python defines how the instance of the class will behave. In order to understand metaclasses well, one needs to have prior experience working with Python classes.

What is class Meta in Django serializer?

What is class Meta in Django serializer? It serves the same point as using a Meta class object inside a Django model class, or a form class, etc. It's a standard pattern to attach configuration (metadata) to a set of fields that make up the model, form, or in this case, serializer.03-Mar-2020.

Does Meta use Django?

In Django, Meta is just used to pass information into the process of creating the _meta Options object.

What is Meta class concept?

In object-oriented programming, a metaclass is a class whose instances are classes. Just as an ordinary class defines the behavior of certain objects, a metaclass defines the behavior of certain classes and their instances. Not all object-oriented programming languages support metaclasses.


2 Answers

Django models use the Meta class to contain extra information about the model that would not necessarily be appropriate to contain within the model class itself. Note that this is not the same as Python's metaclass; that is a completely different topic.

In this case it ordering or sorting the queries to this model by field "Gender"

like image 79
Ignacio Vazquez-Abrams Avatar answered Sep 22 '22 10:09

Ignacio Vazquez-Abrams


Because author/programmer wants to sort results by value of Gender field.

like image 41
bluszcz Avatar answered Sep 24 '22 10:09

bluszcz