Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
In general, I reached the house, I will write in more detail. If you have a field for the user defined in the model, then you can generally select it manually, and if you are too lazy to do this, then you can do this.
Let's say you have an application for publishing articles "articles", in articles/models.py there is an article model that has a field with the user who added it, you can add another field for the user who last edited it, or you can not add it if only the user who added the article is needed:
# остальные импорты опущены
from django.conf import settings
class Article(models.Model):
...
added_by = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True,
related_name="articles_added")
last_edited_by = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True,
related_name="articles_edited")
...
from django.contrib import admin
from .models import Article
class ArticleAdmin(admin.ModelAdmin):
readonly_fields = ("last_edited_by",) # делаем полем рид онли, чтобы нельзя было его отредактировать
def save_model(self, request, obj, form, change):
if change: # True если изменяется уже существующий объект, False если добавляется новый
# определяем и записываем пользователя если изменяется существующий объект
obj.last_edited_by = request.user
else:
# определяем и записываем пользователя если добавляется новый объект
obj.added_by = request.user
super(ArticleAdmin, self).save_model(request, obj, form, change)
admin.site.register(Article, ArticleAdmin)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question