JDavies
JDavies

Reputation: 2770

Customising the Django comment app

I'm having problems with customising the django comments framework. I need to add a 'company' field. I've followed the documentation and not really getting anywhere. It's not far from working because when i add COMMENTS_APP = 'comments_app' to my settings.py the 'comments' app dissapears from admin interface. When i try and write a comment it comes up with the company field where it asks for your email, url etc.

I'd like to be able to view all the comments in the admin panel along with the company field i've added.

Do i need to create a admin.py or have i just got something missing?

Here is my code for my customised comments app:

//MODELS

 from django.db import models
    from django.contrib.comments.models import Comment

    class CommentWithAddedFields(Comment):
        company = models.CharField(max_length=300)

//FORMS.py

from django import forms
from django.contrib.comments.forms import CommentForm
from comments_app.models import CommentWithAddedFields

class CommentFormWithAddedFields(CommentForm):
    company = forms.CharField(max_length=300)


    def get_comment_model(self):

        return CommentWithAddedFields


    def get_comment_create_data(self):

        data = super(CommentFormWithAddedFields, self).get_comment_create_data()
        data['company'] = self.cleaned_data['company']
        return data

//__init.py

from comments_app.models import CommentWithAddedFields
from comments_app.forms import CommentFormWithAddedFields

def get_model():
    return CommentWithAddedFields


def get_form():
    return CommentFormWithAddedFields

I have added the app in the my settings.py file and added COMMENTS_APP = 'comments_app' as i mentioned above.

Am I missed something?

Thanks

Upvotes: 2

Views: 726

Answers (1)

Alasdair
Alasdair

Reputation: 308809

Yes, you need to create an admin.py for your custom comments app if you want your model to appear in the django admin. You should be able to subclass the CommentsAdmin, and customize as required.

from django.contrib import admin
from django.utils.translation import ugettext_lazy as _, ungettext
from django.contrib.comments.admin import CommentsAdmin
from django.contrib.comments import get_model

from comments_app.models import CommentWithAddedFields

class MyCommentsAdmin(CommentsAdmin):
    # Same fieldsets as parent admin, but include 'company'
    fieldsets = (
        (None,
           {'fields': ('content_type', 'object_pk', 'site')}
        ),
        (_('Content'),
           {'fields': ('user', 'user_name', 'user_email', 'user_url', 'company', 'comment')}
        ),
        (_('Metadata'),
           {'fields': ('submit_date', 'ip_address', 'is_public', 'is_removed')}
        ),
     )

# Only register the admin if the comments model is CommentWithAddedFields
# The equivalent section in django.contrib.comments.admin is what prevents 
# the admin from being registered when you set COMMENTS_APP = 'comments_app' 
# in your settings file
if get_model() is CommentWithAddedFields:
    admin.site.register(CommentWithAddedFields, MyCommentsAdmin)

Upvotes: 1

Related Questions