使用 ModelForm 帮助 django bugtrack 评论系统

发布于 2024-10-25 11:20:12 字数 1836 浏览 1 评论 0原文

我正在尝试使用 django 将评论组件添加到错误跟踪应用程序。我有一个用于评论的文本字段和一个按字段——按用户 ID 自动传播。

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

models.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',)

我的 Comment 模型与错误跟踪程序中的 Ticket 模型相关联。我通过将评论放在 admin.py 的内联中来将评论连接到票证。现在的问题是:如何将 ModelForm 传递到 TabularInline 中? TabularInline 需要一个定义的模型。然而,一旦我将模型传递到内联中,传递模型表单就变得没有意义了。

admin.py

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

有谁知道如何将 ModelForm 传递到 TabularInline 而不让常规模型的字段覆盖 ModelForm?提前致谢!

I'm trying to add a comments component to a bug tracking application using django. I have a text field for comments and a by field--auto-propagated by user id.

I want the comments text field to become read-only after someone saves a comment. I've tried doing this several ways. The best way I have come up with so far is to pass my Comment model into a ModelForm and then use form widget attributes to convert my field to read only.

models.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',)

My Comment model is associated with my Ticket model in the bug tracking program. I connect the comments to the tickets by placing the comments in an inline in admin.py. The problem now becomes: how do I pass the ModelForm into a TabularInline? TabularInline demands a defined model. However, once I've passed a model into my inline, passing a model form becomes moot.

admin.py

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

Does anyone know how to pass a ModelForm into a TabularInline without having a regular Model's fields override the ModelForm? Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长途伴 2024-11-01 11:20:12

不要在 TabularInline 子类中实例化表单:

    form = CommentForm

Don't instantiate the form in the TabularInline subclass:

    form = CommentForm
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文