使用ModelForm帮助Django错误跟踪注释系统

2024-04-20 08:03:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用django向bug跟踪应用程序添加注释组件。我有一个用于注释的文本字段和一个按字段——按用户id自动传播

我希望有人保存评论后,评论文本字段变为只读。我试过几种方法。到目前为止,我想到的最好的方法是将注释模型传递到ModelForm中,然后使用表单小部件属性将字段转换为只读。你知道吗

你知道吗型号.py你知道吗

class CommentForm(ModelForm):                                                 
    class Meta: 
        model = Comment
        exclude = ('ticket', 'submitted_date', 'modified_date')               
    def __init__(self, *args, **kwargs):
        super(CommentForm, self).__init__(*args, **kwargs)                    
        instance = getattr(self, 'instance', None)                            
        if instance and instance.id:
            self.fields['comments'].widget.attrs['readonly'] = True           

class Comment(models.Model):
    ticket = models.ForeignKey(Ticket)
    by = models.ForeignKey(User, null=True, blank=True, related_name="by")    
    comments = models.TextField(null=True, blank=True)
    submitted_date = models.DateField(auto_now_add=True)                      
    modified_date = models.DateField(auto_now=True)                           
    class Admin:
        list_display = ('comments', 'by',
            'submitted_date', 'modified_date')
        list_filter = ('submitted_date', 'by',)                               
        search_fields = ('comments', 'by',)

我的评论模型与bug跟踪程序中的票证模型相关联。我通过将注释放置在inline-in中,将注释连接到票证管理员.py. 现在的问题是:如何将ModelForm传递到TabularInline中?TabularInline需要一个定义的模型。然而,一旦我将模型传递到内联中,传递模型表单就变得毫无意义了。你知道吗

你知道吗管理员.py你知道吗

class CommentInline(admin.TabularInline):
    model = Comment
    form = CommentForm()
    search_fields = ['by', ]
    list_filter = ['by', ]
    fields = ('comments', 'by')
    readonly_fields=('by',)
    extra = 1

有人知道如何将模型形式传递到表格行中,而不让常规模型的字段覆盖模型形式吗?提前谢谢!你知道吗


Tags: instancepy模型selftruefieldsdateby