views:

22

answers:

1

I have created a simple project where everyone can create one or more Blog. I want to use this models for Post and for Comment:

class Post_comment(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField(_('object ID'))
    content_object = generic.GenericForeignKey()

    # Hierarchy Field
    parent = models.ForeignKey('self', null=True, blank=True, default=None, related_name='children')

    # User Field
    user = models.ForeignKey(User)

    # Date Fields
    date_submitted = models.DateTimeField(_('date/time submitted'), default = datetime.now)
    date_modified = models.DateTimeField(_('date/time modified'), default = datetime.now)

    title = models.CharField(_('title'), max_length=60, blank=True, null=True)  
    post_comment = models.TextField(_('post_comment'))

if it is a comment the parent is not null. So in most case the text field will contain a little bit of text. Can I use this model for both Post and Comment ? Is it a good solution ?

A: 

Its technically possible, but sounds like a bad solution.

Most of the queries you'll run are specific to either post or comment (examples: get all posts ordered by date to show on the blog index page, get 5 most recent posts' titles to show on a widget, get 5 most recent comments to show in a "latest comments" widget, get all comments of a specific post, get all the posts that a user has posted, etc). So the cost of having them in the same table is always having the .filter(parent=None) which means less readable code and some performance loss.

Ofri Raviv
mmm so I could use Post model with a foreign key to blog and a Comment model with a generic foreign key ( because I could comment post, images, videos, ecc ). Is this a wrong ? What is the better solution in your opinion ? Thanks ^_^
xRobot
yea, exactly - 2 models. Post with FK to blog, and Comment (maybe use django.contrib.comments?) with generic FK.
Ofri Raviv