Skip to content
Related Articles

Related Articles

Improve Article

Meta Class in Models – Django

  • Last Updated : 16 Mar, 2021

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.

Check More About Django models here – Django Models

What is Model Meta?

Model Meta is basically the inner class of your model class.

Model Meta is basically used to change the behavior of your model fields like changing order options,verbose_name and lot of other options. It’s completely optional to add Meta class in your model.



To use model meta you have to add class Meta in your model like this

class student(models.Model):
    class Meta:
        options........

Model Meta Options

Model Meta have a lot of options that you can give your model in its internal class meta

1. abstract

If abstract = True , this model will be an abstract  base class

Python3




class student(models.Model):
  class Meta:
      abstract = True

2. app_label

If a model is defined outside of an applications in INSTALLED_APPS, it must declare which app  it belongs to:



Python3




class student(models.Model):
  class Meta:
      app_label = 'myapp' # add app name here

3. verbose_name

verbose_name is basically a human-readable name for your model

Python3




class student(models.Model):
  class Meta:
      verbose_name = "stu" # add verbose_name  here

4. ordering 

Ordering is basically is used to change the order of your model fields.

Python3




class student(models.Model):
  class Meta:
      ordering = [-1]

Add ordering like this [-1] it change the order in descending order

5. proxy

If we add proxy = True a model which subclasses another model will be treated as a proxty model

Python3




class Teacher(models.Model):
  pass
  
class Student(Teacher):
  class Meta:
      proxy = True

This is how can we make a  proxy model.

6. permissions 

Extra permissions to enter into the permissions table when creating this object. Add, change ,delete and view permissions are automatically created for each model.

Python3




class student(models.Model):
  class Meta:
      permissions = []
     

You can add extra permission inside the list.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :