Django Comments: Want to remove user URL, not expand the model. How to?

前端 未结 3 949
礼貌的吻别
礼貌的吻别 2020-12-31 08:36

I\'m totally understanding the documentation on expanding the Comments app in Django, and really would like to stick with the automatic functionality but...

3条回答
  •  执笔经年
    2020-12-31 08:51

    I can't comment onto SmileyChris' post for some reason, so I am going to post it here. But, I ran into errors using just SmileyChris' response. You also have to overwrite the get_comment_create_data function because CommentForm is going to look for those Post keys that you removed. So here's my code after I removed three fields.

    class SlimCommentForm(CommentForm):
    """
    A comment form which matches the default djanago.contrib.comments one, but with 3 removed fields
    """
    def get_comment_create_data(self):
        # Use the data of the superclass, and remove extra fields
        return dict(
            content_type = ContentType.objects.get_for_model(self.target_object),
            object_pk    = force_unicode(self.target_object._get_pk_val()),
            comment      = self.cleaned_data["comment"],
            submit_date  = datetime.datetime.now(),
            site_id      = settings.SITE_ID,
            is_public    = True,
            is_removed   = False,
        )
    
    
    SlimCommentForm.base_fields.pop('url')
    SlimCommentForm.base_fields.pop('email')
    SlimCommentForm.base_fields.pop('name')
    

    This is the function that you are overwriting

    def get_comment_create_data(self):
        """
        Returns the dict of data to be used to create a comment. Subclasses in
        custom comment apps that override get_comment_model can override this
        method to add extra fields onto a custom comment model.
        """
        return dict(
            content_type = ContentType.objects.get_for_model(self.target_object),
            object_pk    = force_unicode(self.target_object._get_pk_val()),
            user_name    = self.cleaned_data["name"],
            user_email   = self.cleaned_data["email"],
            user_url     = self.cleaned_data["url"],
            comment      = self.cleaned_data["comment"],
            submit_date  = datetime.datetime.now(),
            site_id      = settings.SITE_ID,
            is_public    = True,
            is_removed   = False,
        )
    

提交回复
热议问题