2011-03-22 57 views
0

我想添加一个评论组件到使用Django的错误跟踪应用程序。我有一个评论和一个字段的文本字段 - 自动传播的用户ID。帮助与Django的bugtrack评论系统使用ModelForm

我希望评论文本字段在某人保存评论后变为只读。我尝试了几种方法。到目前为止,我提出的最佳方法是将我的评论模型传递给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',) 

我的评论模型与在bug跟踪程序我的票模型相关联。我通过在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?提前致谢!

回答

1

不要实例的形式在TabularInline子类:

form = CommentForm 
+0

谢谢!这绝对是一个好主意,但我的表单仍然不会覆盖admin.TabularInline中的模型。 – Zach 2011-03-23 13:12:22