This article example describes the Django framework custom model manager and meta-option usage. Shared for your reference, as follows:
Custom Model Manager
Each model class has an objects class attribute by default, which can be called the model manager. It is automatically generated by django and is of type
You can customize the model manager in the model class, after customization, Django will no longer generate the default objects. (The model class can be customized with multiple managers.)
Example:
class Department(): # Customized Model Manager manager = ()
call throws an AttributeError exception, and () returns a list of all Department objects.
Two cases require a custom manager
The original query set returned by the Modification Manager
Requirement: Call()
Returns are to departments established after 2009
Add additional methods to the manager class to help us manipulate the data table corresponding to the model class
Requirement: In the Manager class, define a method to create a department.
I. Customizing the Model Manager
class DepartmentManager(Manager): # Modify the original query set returned by the Manager def all(self): """Rewrite all method: return only departments created after 2009""" return super().all().filter(create_date__gte=date(2009,1,1)) # Encapsulate additions, deletions and checks in the Model Manager def create_dep(self, name, create_date): """Add a new sector""" dep = Department() = name dep.create_date = create_date () return dep # Return the added employee object
II. Using custom model managers in model classes
class Department(): """Sectoral categories""" ... # Customize the model manager (default objects will not be generated anymore) objects = DepartmentManager()
III. In the view function, use the methods in the customized model manager
def add_dep(request): """Additional sectors""" # d = Department() # = 'Finance Department' # d.create_date = date(2018, 1, 1) # () # Call up the custom model manager and add a new department .create_dep('Ministry of Finance', date(2018, 1, 1)) # Returns no longer all sectors but those created after 2009 # () return redirect('/show_deps')
Meta option (Meta)
I. Modify the table name.
The name of the table that Django generates by default:
Application Name Lowercase_Model Class Name Lowercase
You can modify the table name by defining the Meta class in the model class:
class Department(): """Sectoral categories""" name = (max_length=20) class Meta(object): """Specify table name""" db_table = "department"
Re-generate the migration file and migrate the generated table to see if the table name has been modified.
II. Modify the display name of the model class in the management background
class Meta: # Define the table name db_table = 'department' # Define the name to be displayed in the admin backend verbose_name = 'Sector' # Name when specifying plurals (removing the plural s) verbose_name_plural = verbose_name
I hope that what I have said in this article will help you in designing Python programs based on Django framework.