2011-03-22 40 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