简体   繁体   English

如何保存在 django 管理站点中创建和修改记录的用户?

[英]How to save user that created and modified a record in django admin site?

I have a ReportModel in my django app with 2 field create_user (represents the user that created the report) and write_user (represents the user that last modified the report).我的 django 应用程序中有一个 ReportModel,其中有 2 个字段 create_user(代表创建报告的用户)和 write_user(代表最后修改报告的用户)。 I want to automatically save that two fields according to the user that is logged in on django admin site.我想根据在 django 管理站点上登录的用户自动保存这两个字段。 How do I do that?我怎么做? Here is the definition of the model这是model的定义

class ReportModel(models.Model):


    name = models.CharField(verbose_name=_("Nombre"), max_length=50, blank=False, null=False)
    location = models.PointField(verbose_name=_("Localización"), srid=4326, blank=False, null=False)
    report_type_id = models.ForeignKey("ReportTypeModel", verbose_name=_("Tipo"),
                                                 blank=True, null=True, on_delete=models.SET_NULL,
                                                 related_name="reports")
    start_date = models.DateField(verbose_name=_("Fecha inicio"))
    end_date = models.DateField(verbose_name=_("Fecha fin"))
    create_user = models.ForeignKey(User, on_delete=models.CASCADE, 
                                   related_name='+', verbose_name=_('Creado por'), editable=False, null=True, blank=True)

    write_user = models.ForeignKey(User, on_delete=models.CASCADE, 
                                   related_name='+', verbose_name=_('Modificado por'), editable=False, null=True, blank=True)

    def __str__(self):
        return self.name

you can override the create and update methods in your serializer.您可以覆盖序列化程序中的创建和更新方法。 In the methods before you call the super class update and create methods, you can add the fiels by your self from the request.user在调用超级 class 更新和创建方法之前的方法中,您可以在 request.user 中自行添加字段

something like就像是

def create(self, validated_data):
        """
        Overriding the default create method of the Model serializer.
        :param validated_data: data containing all the details of your model
        :return: returns a successfully created record
        """
        validated_data.update({"create_user": request.user})
        # call super class create method here by passing the modified validated_data
        return student

In order to capture/record the user who performed a create/update on a model in the django admin, override the save_model method on the admin view.为了捕获/记录在 django 管理员中对 model 执行创建/更新的用户,请覆盖管理员视图上的 save_model 方法。 In the ReportModel admin view declared in admin.py file, override the save_model method as follows:在 admin.py 文件中声明的 ReportModel 管理视图中,覆盖 save_model 方法,如下所示:


from models import ReportModel
from django.contrib import admin

@admin.register(ReportModel)
class ReportModelAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        if not obj.pk:
            obj.create_user = request.user #create_user should only be set once
        obj.write_user = request.user #write_user can be set at all times
        super().save_model(request, obj, form, change)


Reference: How to associate model with current user while saving参考: 如何在保存时将 model 与当前用户关联

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM