在内联管理中自动生成字段

时间:2021-10-01 16:16:30

I have these (simplified) models:

我有这些(简化的)模型:

class User(models.Model):
    email = models.EmailField(unique=True)

class Invitation(models.Model):
    user = models.ForeignKey(User)
    path = models.CharField(max_length=40, unique=True)

The path field in the Invitation table will contain a SHA1 hash that is going to be used as part of the URL to access the user's data.

邀请表中的path字段将包含一个SHA1散列,该散列将用作访问用户数据的URL的一部分。

I have this admin code:

我有这个管理代码:

class InvitationInline(admin.TabularInline):
    model = models.Invitation

class UserAdmin(admin.ModelAdmin):
    inlines = (InvitationInline,)

admin.site.register(models.User, UserAdmin)

this displays the user and adds a list of invitations at the bottom.

这将显示用户并在底部添加邀请列表。

Since my path values in the Invitation table are going to be generated by SHA1 algorithm from the user's email and the current timestamp, I need to:

由于我在邀请表中的路径值将由SHA1算法从用户的邮件和当前的时间戳中生成,我需要:

  1. have no empty 'Invitations' rows displayed by default in the User admin
  2. 在用户管理中默认显示没有空的“邀请”行?
  3. remove the edit field for path column from the admin, and have the path field automatically generated when the "add another invitation" button is clicked.
  4. 从admin中删除path列的edit字段,并在单击“添加另一个邀请”按钮时自动生成path字段。

I have no idea how to achieve this, could someone help me?

我不知道该怎么做,有人能帮我吗?

1 个解决方案

#1


1  

  1. To disable display of extra inline invitation forms just add extra = 0 attribute to your InvitationInline class:

    要禁用显示额外的内联邀请表单,只需向邀请人内联类添加额外的= 0属性:

    class InvitationInline(admin.TabularInline):
        fields = ('user',)
        extra = 0
        model = models.Invitation
    
  2. Probably, the best place to put the path generation is the save method of Invitation model:

    可能,放置路径生成的最佳位置是邀请模型的保存方法:

    import hashlib
    import random
    
    class Invitation(models.Model):
        user = models.ForeignKey(User)
        path = models.CharField(max_length=40, unique=True)
    
        def save(self, *args, **kwargs):
            if self.pk is None:  # This is true only when the model has
                                 # never been saved to database.
                salt = hashlib.sha1(str(random.random())).hexdigest()[:5]
                self.path = hashlib.sha1(salt +\
                    str(self.user.email)).hexdigest()
    
            super(Invitation, self).save(*args, **kwargs)  
    

To remove path from InvitationInline just add fields attribute to it: fields = ('user',).

要从InvitationInline删除路径,只需向其添加fields属性:fields = ('user',)。

#1


1  

  1. To disable display of extra inline invitation forms just add extra = 0 attribute to your InvitationInline class:

    要禁用显示额外的内联邀请表单,只需向邀请人内联类添加额外的= 0属性:

    class InvitationInline(admin.TabularInline):
        fields = ('user',)
        extra = 0
        model = models.Invitation
    
  2. Probably, the best place to put the path generation is the save method of Invitation model:

    可能,放置路径生成的最佳位置是邀请模型的保存方法:

    import hashlib
    import random
    
    class Invitation(models.Model):
        user = models.ForeignKey(User)
        path = models.CharField(max_length=40, unique=True)
    
        def save(self, *args, **kwargs):
            if self.pk is None:  # This is true only when the model has
                                 # never been saved to database.
                salt = hashlib.sha1(str(random.random())).hexdigest()[:5]
                self.path = hashlib.sha1(salt +\
                    str(self.user.email)).hexdigest()
    
            super(Invitation, self).save(*args, **kwargs)  
    

To remove path from InvitationInline just add fields attribute to it: fields = ('user',).

要从InvitationInline删除路径,只需向其添加fields属性:fields = ('user',)。