JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Metaclasses in Django models

Author:JIYIK Last Updated:2025/05/03 Views:

Metadata refers to a specific set of data that provides information about another data. In Django, we use Django models to design our database's tables and their fields. If we have to add some data about the model itself, we use classes. Learn more about classes Metain Django models in this article .Meta


MetaClasses in Django

MetaThe class is an inner class, which means it is defined inside the model as follows:

from django.db import models


class MyModel(models.Model):
    ...

    class Meta:
        ...

MetaClasses can be used to define various things about a model, such as permissions, database names, singular and plural names, abstractions, ordering, etc. Adding Metaclasses to a Django model is completely optional.

This class also comes with a number of options you can configure. Here are some commonly used meta options; you can explore all meta options here

Django meta options - summary

This option is used to define whether the model is abstract; they work the same way as abstract classes. Abstract classes are classes that cannot be instantiated, only extended or inherited.

Models set to abstract can only be inherited. This option can be used if there are multiple models with common fields.

from django.db import models


class Human(models.Model):
    genders = (
        ("M", "Male"),
        ("F", "Female"),
        ("NB", "Non-binary"),
        ("T", "Transgender"),
        ("I", "Intersex"),
        ("O", "Other"),
        ("PNTS", "Prefer not to say"),
    )

    name = models.CharField(max_length=200)
    age = models.IntegerField(default=0)
    gender = models.CharField(max_length=50, choices=genders)

    class Meta:
        abstract = True  # Important


class Teacher(Human):
    subject = models.CharField(max_length=200)


class Student(Human):
    grade = models.IntegerField(default=0)

Here, Teacherthe and Studentmodels will contain Humanall the fields from the model. In the database, only the Teacherand Studentmodels will be created.

Django meta options -db_table

This option is used to set the name used to identify the table within the database. For example: If I do the following, the name of my model will be in the database job.

from django.db import models


class JobPosting(models.Model):
    class Meta:
        db_table = "job"

Django meta options - sorting

This option takes a list of string values, which are the model fields. It is used to define the ordering of model objects. When objects of this model are retrieved, they will appear in this order.

from django.db import models


class JobPosting(models.Model):
    dateTimeOfPosting = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ["-dateTimeOfPosting"]

In the example above, the retrieved objects will dateTimeOfPostingbe sorted in descending order based on the field. ( -The prefix is ​​used to define descending order.)

Django meta options -verbose_name

This option is used to define a human-readable singular name for the model, and will override Django's default naming convention. This name will also be reflected in the admin panel ( /admin/).

from django.db import models


class JobPosting(models.Model):
    class Meta:
        verbose_name = "Job Posting"

Django meta option - Verbose_name_plural

This option is used to define a human-readable plural name for the model, which again overrides Django's default naming convention. This name will also be reflected in the admin panel ( /admin/).

from django.db import models


class JobPosting(models.Model):
    class Meta:
        verbose_name_plural = "Job Postings"

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Convert Tensor to NumPy array in Python

Publish Date:2025/05/03 Views:85 Category:Python

This tutorial will show you how to convert a Tensor to a NumPy array in Python. Use the function in Python Tensor.numpy() to convert a tensor to a NumPy array Eager Execution of TensorFlow library can be used to convert tensor to NumPy arra

Saving NumPy arrays as images in Python

Publish Date:2025/05/03 Views:193 Category:Python

In Python, numpy module is used to manipulate arrays. There are many modules available in Python that allow us to read and store images. An image can be thought of as an array of different pixels stored at specific locations with correspond

Transposing a 1D array in NumPy

Publish Date:2025/05/03 Views:98 Category:Python

Arrays and matrices form the core of this Python library. The transpose of these arrays and matrices plays a vital role in certain topics such as machine learning. In NumPy, it is easy to calculate the transpose of an array or a matrix. Tra

Find the first index of an element in a NumPy array

Publish Date:2025/05/03 Views:58 Category:Python

In this tutorial, we will discuss how to find the first index of an element in a numpy array. Use where() the function to find the first index of an element in a NumPy array The function in the numpy module where() is used to return an arra

Remove Nan values from NumPy array

Publish Date:2025/05/03 Views:118 Category:Python

This article discusses some built-in NumPy functions that you can use to remove nan values. Remove Nan values ​​using logical_not() and methods in NumPy isnan() logical_not() is used to apply logical NOT to the elements of an array. isn

Normalizing a vector in Python

Publish Date:2025/05/03 Views:51 Category:Python

A common concept in the field of machine learning is to normalize a vector or dataset before passing it to the algorithm. When we talk about normalizing a vector, we say that its vector magnitude is 1, being a unit vector. In this tutorial,

Calculating Euclidean distance in Python

Publish Date:2025/05/03 Views:128 Category:Python

In the world of mathematics, the shortest distance between two points in any dimension is called the Euclidean distance. It is the square root of the sum of the squares of the differences between the two points. In Python, the numpy, scipy

Element-wise division in Python NumPy

Publish Date:2025/05/03 Views:199 Category:Python

This tutorial shows you how to perform element-wise division on NumPy arrays in Python. NumPy Element-Wise Division using numpy.divide() the function If we have two arrays and want to divide each element of the first array with each element

Convert 3D array to 2D array in Python

Publish Date:2025/05/03 Views:79 Category:Python

In this tutorial, we will discuss the methods to convert 3D array to 2D array in Python. numpy.reshape() Convert 3D array to 2D array using function in Python [ numpy.reshape() Function](numpy.reshape - NumPy v1.20 manual)Changes the shape

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial