djangodjango-modelsdjango-formsdjango-comments

CommentForm design question about Django-comments package


In Django's own comment framework, django-contrib-comments, it has below class defined:

  1. In models.py, class Comment (CommentAbstractModel): is defined.

  2. In forms.py, class CommentForm (CommentDetailsForm): is defined.

Q: since the comment framework is a comment-db-table driven package, why not we use ModelForm to create forms from models, instead of redundantly create another CommentForm class ?


Solution

  • The class CommentDetailsForm inherits from CommentSecurityForm, which defines various util methods and performs custom validation. These forms also declare fields that are not contained in the Comment model. Yes, it was possible to use ModelForm instead of Form, but I think that this decision is not wrong because these forms need specific behaviors, ModelForm would have saved some lines of code (not many), but the actual implementation is more explicit. Using ModelForm with models is not a must.