开发者

Help with django bugtrack comments system using 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 c开发者_Go百科omment. 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!


Don't instantiate the form in the TabularInline subclass:

    form = CommentForm
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜