Hi. I am asking this question after reading the following questions: Question_1 and Question_2. Question_1 doesn't have any appropriate answers and Question_2 is having an alternative solution but not a perfect solution.
你好。在阅读以下问题后,我问这个问题:Question_1和Question_2。 Question_1没有任何适当的答案,Question_2有一个替代解决方案,但不是一个完美的解决方案。
Here I have two models and admins for them.
在这里,我有两个模型和管理员。
models.py
models.py
class TaskList(models.Model):
task_name = models.CharField(max_length = 255, unique = True)
description = models.TextField()
assignee_role = models.ForeignKey(Group, related_name = "assignee_roles")
assignee_name = models.ForeignKey(User, related_name = "assignee_names")
def __unicode__(self):
return "%s" % (self.task_name)
class TaskComments(models.Model):
tasklist = models.ForeignKey(TaskList)
time = models.DateTimeField(null = True, blank = True)
comment = models.TextField()
def __unicode__(self):
return ""
def save(self, *args, **kwargs):
self.time = datetime.datetime.now()
super(TaskComments, self).save(*args, **kwargs)
admin.py
admin.py
class TaskCommentsInlineAdmin(admin.TabularInline):
model = TaskComments
can_delete = False
class TaskListAdmin(admin.ModelAdmin):
inlines = [TaskCommentsInlineAdmin, ]
def add_view(self, request, form_url = '', extra_context = None):
self.readonly_fields = ()
return super(TaskListAdmin, self).add_view(request, form_url, extra_context)
def change_view(self, request, form_url = '', extra_context = None):
self.readonly_fields = ('task_name', 'description', )
return super(TaskListAdmin, self).change_view(request, form_url, extra_context)
Here the model TaskComments is being used as inline in TaskList.
这里,模型TaskComments被用作TaskList中的内联。
SCENARIO 1
情景1
Here what I want to achieve is:
这里我想要实现的是:
When Adding task comments
添加任务注释时
- Hide the field "time" and update it in backend.
- 隐藏字段“time”并在后端更新它。
- Able to type "comments" and save.
- 能够输入“评论”并保存。
When opening after saving comments
保存评论后打开时
- Both "Time" and "Comments" field should be visible and readonly for already added comments.
- 对于已添加的注释,“时间”和“注释”字段都应该是可见的和只读的。
- Able to add new comments as described above.
- 能够如上所述添加新评论。
SCENARIO 2
情景2
- Same as previous scenario.
- 与之前的方案相同。
- The only addition is, the comment should be editable for the user who added it and readonly for all other users. (I can update and get the user who adds the comment).
- 唯一的补充是,评论应该为添加它的用户和所有其他用户只读编辑。 (我可以更新并获得添加评论的用户)。
These are the things I would like to achieve. I have tried using add_view and change_view in admin. But they are not working for inline. Please provide a solution to achieve these things. Thanks in advance.
这些是我想要实现的目标。我尝试在admin中使用add_view和change_view。但他们不是为内联工作。请提供实现这些目标的解决方案。提前致谢。
1 个解决方案
#1
0
You have to use the custom version InlineAdmin which uses the custom version of below classes
您必须使用自定义版本InlineAdmin,它使用以下类的自定义版本
- InlineModelAdmin
- InlineModelAdmin
- BaseInlineFormSet
- BaseInlineFormSet
You can add your own add_view and change_view on the custom version of InlineModelAdmin to
您可以在InlineModelAdmin的自定义版本上添加自己的add_view和change_view
- detect the owner of the record(to allow the edit)
- 检测记录的所有者(允许编辑)
- display the time field
- 显示时间字段
You can enhance the BaseInlineFormSet to control all the runtime aspects
您可以增强BaseInlineFormSet来控制所有运行时方面
#1
0
You have to use the custom version InlineAdmin which uses the custom version of below classes
您必须使用自定义版本InlineAdmin,它使用以下类的自定义版本
- InlineModelAdmin
- InlineModelAdmin
- BaseInlineFormSet
- BaseInlineFormSet
You can add your own add_view and change_view on the custom version of InlineModelAdmin to
您可以在InlineModelAdmin的自定义版本上添加自己的add_view和change_view
- detect the owner of the record(to allow the edit)
- 检测记录的所有者(允许编辑)
- display the time field
- 显示时间字段
You can enhance the BaseInlineFormSet to control all the runtime aspects
您可以增强BaseInlineFormSet来控制所有运行时方面