In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the relevant knowledge of "Django record operation log and what is the use of LogEntry". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "what is the use of Django record operation log and LogEntry" can help you solve the problem.
1. Use LogEntry
ModelAdmin itself has logging capabilities. When creating a new entity (Post, Category, Tag), ModelAdmin creates a change log record. When a piece of content is modified, ModelAdmin calls LogEntry to create a log to record the change.
Two methods are provided internally in ModelAdmin, log_addition and log_change.
Log is added to log_addition record.
Log_change records the change log.
We can learn the LogEntry module by looking at their definitions.
Code location: django/admin/contrib/options.py
Def log_addition (self, request, object, message): "Log that an object has been successfully added. The default implementation creates an admin LogEntry object." From django.contrib.admin.models import LogEntry, ADDITION return LogEntry.objects.log_action (user_id=request.user.pk, content_type_id=get_content_type_for_model (object). Competition, object_id=object.pk, object_repr=str (object), action_flag=ADDITION, change_message=message,) def log_change (self, request, object Message): "" Log that an object has been successfully changed. The default implementation creates an admin LogEntry object. " From django.contrib.admin.models import LogEntry, CHANGE return LogEntry.objects.log_action (user_id=request.user.pk, content_type_id=get_content_type_for_model (object). Competition, object_id=object.pk, object_repr=str (object), action_flag=CHANGE, change_message=message,)
As you can see from the above code: both methods call the LogEntry.objects.log_action method, but the parameters are slightly different. You can see that if you need to customize the change record, you only need to pass the corresponding parameters. These parameters are briefly described below.
User_ id
Current user id.
Content_type_id
To save the type of content, the above code uses the get_.content_type_for_model method to get the type id of the corresponding Model. It's easy to understand that ContentType defines a type id for each Model.
Object_id
Record the id of the change instance, for example, in PostAdmin it is post. Id .
Object_repr
The display name of the instance can be simply understood as the content returned by the _ _ str__ we defined.
Action flag
Operation mark. Several basic tags are defined in admin's Model: ADDITION, CHANGE, and DELETION. It is used to mark whether the current parameter is changed, added, or deleted.
Change_ message
This is a recorded message and can be defined by yourself. We can put in the newly added content (we can restore it here if necessary), or we can put in the difference between the old and the new.
With an understanding of these parameters, if you encounter similar requirements, you can directly use Django's off-the-shelf tools to do so.
two。 Query for changes to an object
Now that we know how to log changes to an object, the question is, how to inquire about the changes that have been recorded?
This is actually a simple Model query problem. Assuming that the object we recorded is the operation of Post, now let's get all the change logs with id 1 in Post. The code is as follows:
From django.contrib.admin.models import LogEntry, CHANGEfrom django.contrib.admin.options import get_content_type_for_modelpost = Post.objects.get (id=1) log_entries = LogEntry.objects.filter (content_type_id=get_content_type_for_model (post). Competition, object_id=post.id
In this way, we have all the change records for the article with id 1.
3. View the operation log on the admin page
We know both how to log changes and how to get them, so how can we easily view the operation log in the admin background?
The following configurations are added:
# add importfrom django.contrib.admin.models import LogEntry# file at the top and @ adnin.register (LogEntry, site=custom_site) class LogEntryAdmin (admin.ModelAdmin) at the bottom: list_display = ['object_repr','object_ id','action_flag','user','change_message']
If you have configured xadmin, configure it in adminx.py:
# add importfrom django.contrib.admin.models import LogEntry# file at the top and add xadmin.site.register (LogEntry,LogEntryAdmin) class LogEntryAdmin (object) at the bottom: list_display = ['object_repr','object_id','action_flag','user','change_message']
So you can see all the change records. As shown in the following figure:
This is the end of the content about "what is the use of Django record operation log and LogEntry". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.