In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to use django2.0 to develop ModelAdmin model management, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Run Development Server > cd cms > python manager.py runserver
Open the browser http://127.0.0.1:8000/admin
You can see the login window in the background.
Create a Super Admin
Since we don't have a super administrator in the background, first create one from the command line
> cd cms > python manager.py createsuperuser
Follow the prompts to enter Username and Password. Enter the account password in the browser and log in to the background.
Register the Account module in the background
After logging in successfully, we found that we didn't have our Account module. Now let's register the Account module in the background.
# account/admin.pyfrom django.contrib import adminfrom .models import Accountadmin.site.register (Account)
Then refresh the browser, you will find that the Account module appears, but the interface displayed is all in English.
There is also a way to register decorators.
# account/admin.pyfrom django.contrib import adminfrom .models import Account@admin.register (Account) class AccountAdmin (admin.ModelAdmin): pass
Will achieve the same effect.
The Chinese culture of django
Of course django thought of that for us.
# cms/settings.py# specified language is Chinese LANGUAGE_CODE = 'zh-Hans'# specified time zone TIME_ZONE =' PRC'# is changed to False, and the time stored in the database is normal. Otherwise, it will be eight hours less than normal. USE_TZ = False uses ModelAdmin to modify the management interface and operations # account/admin.pyfrom django.contrib import adminfrom. Models import Account@admin.register (Account) class AccountAdmin (admin.ModelAdmin): # controls the list page button display location actions_on_top = False actions_on_bottom = True # whether the list page data quantity ([m of n selected]) actions_selection_counter = True is displayed
ModelAdmin is a control class of the background specified module, including the background list page style, data, editing / adding, operation methods, etc. are all controlled by this class. When inheriting this class, we can complete some of our operations by overriding the methods of this class.
Configuration parameters of ModelAdmin
Actions
Specify a list of custom actions
If it is a defined function, then
Actions = [function name]
If it is the function of class, then
Actions = ['method name in class']
Actions_on_top
Specify whether the button of the list page is above, or set to True
Actions_on_bottom
The same as actions_on_top, but whether it is below or not
Date_hierarchy
It only works on DateField and DateTimeField. After setting it, you can filter the data according to the time period.
Class AccountAdmin (models.ModelAdmin):... Ignore other code date_hierarchy = 'create_time'
Exclude
Exclude fields that need to be displayed on the new / edit page
Fields
Specify the fields to be displayed on the new and editing page
Fields = ('account',' nickname', 'password',' email', 'phone',' status')
List_display
Controls the fields to be displayed on the list page
List_display = ('account',' nickname', 'email',' phone', 'status',' create_time')
List_display_links
Specify which fields are linked to the list page
List_display_links = ('account',)
List_editable
Specify which fields can be edited directly on the list page
List_editable = ('status',)
List_filter
List page filter criteria
List_filter = ('status',)
Ordering
Sorting, affecting order_by
Ordering = ['id]
List_per_page
Control the amount of data displayed per page
ModelAdmin adds custom operations
The status of each member is enabled and disabled, and now we want to add an operation to enable and disable in bulk.
Define a method of operation first
Class AccountAdmin (models.ModelAdmin): def disable_account (self, request, queryset): queryset.update (status=0) disable_account.short_description=' enable'
Disable_account contains two parameters
Request is a HttpRequest object that contains all the clarity of the request
Queryset is the QuerySet object of the database operation, that is, when we click the operation button, we will execute queryset.filter (id selected by id__in=) .update (status=0). Django has already written queryset.filter () for us in advance.
Disable_account.short_description is the name that specifies that the operation is displayed in the background
After the method is defined, the method of the operation needs to be written to the actions property before it takes effect.
Class AccountAdmin (models.ModelAdmin): actions = ['disable_ccount'] def disable_account (self, request, queryset): queryset.update (status=0) disable_account.short_description=' enable'
Again, we can define another enable_accoun method.
Then in the actions on the list page, you can see the actions we defined.
Another way to add custom operations
This method can be used in general methods. For example, our account model has an is_deleted field, which means that after the data is deleted, the is_deleted is set to 1, which is 0 by default, while the default delete operation of django is to delete the data directly, so we need to override the deletion method.
# account/admin.pyfrom django.contrib import admin# disables the default delete operation admin.site.disable_action ('delete_selected')
This disables the default delete operation
# redefine a delete operation def deleted_select (self, request, queryset): queryset.update (is_deleted=1)
The principle is the same as before we added the method of operation.
Register this operation with the admin site
Admin.site.add_action (deleted_select, 'delete data')
The first argument to add_action is the name of the operation's function, and the second is the button displayed.
Modify the default data query method of ModelAdmin
After we modified the delete operation, we found that even if we deleted the data, it would still appear in the list, which was not in line with our expectations.
So we need to rewrite ModelAdmin's query data method to filter out is_deleted=1 data.
Class AccountAdmin (admin.ModelAdmin):... Omit the code def get_queryset (self, request): queryset = super () .get_queryset (request) return queryset.filter (is_deleted=0)
The first step, queryset = super () .get_queryset (request), is to get the query object QuerySet of ModelAdmin, then add the filter condition filter, and then ModelAdmin will continue to operate on the query method.
These are all the contents of the article "how to use django2.0 to develop ModelAdmin Model Management". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.