I have this models:
class Comment(models.Model):
text = models.TextField(max_length = 300)
author = models.ForeignKey(User)
timestamp = models.DateTimeField(auto_now_add = True)
class UserProfile(models.Model):
user = models.ForeignKey(User, unique = True)
comments = models.ManyToManyField(Comment)
class Product(models.Model):
title = models.CharField(max_length = 30)
comments = models.ManyToManyField(Comment)
I know there’s django.contrib.comments but now I’m writing my own comments system.
Either UserProfile and Product object can have a list of comments.
Is it logically correct this?
My doubt is: a ManyToManyField means:
- an object A has many objects B, so an object B has many objects A
- or many objects A has many objects B ?
Which one it’s the correct sentence? Because if it is the first one, my models layout it’s wrong, because (for example) a Product has many Comments but a Comment has NOT many Product.
Can you clarify my doubt?
Your first statement is correct, for a ManyToManyField “an object A has many objects B, so an object B has many objects A”
Note that when you define
There is a kind of implicit ManyToManyField defined on Comment for UserProfile, eg
In fact you can define the many to many table either way round.
As you’ve noted your model definition doesn’t work with two ManyToManyFields. What you want to use is a GenericForeignKey which can attach anything to anything else (which is how the commenting framework works IIRC).
Something like