class WallPost(models.Model):
text = models.TextField()
class UserProfile(models.Model):
name = models.CharField(max_length=128)
wall_posts = models.ManyToManyField(WallPost, through='UserWall')
class UserWall(models.Model):
profile = models.ForeignKey(UserProfile)
post = models.ForeignKey(WallPost)
#same for groups
class Group(models.Model):
name = models.CharField(max_length=128)
wall_posts = models.ManyToManyField(WallPost, through='GroupWall')
class GroupWall(models.Model):
group = models.ForeignKey(Group)
post = models.ForeignKey(WallPost)
UserWall.objects.filter(profile_id=profile.id).select_related('post')
GroupWall.objects.filter(group_id=group.id).select_related('post')
#or
group = Group.objects.get(id=1).select_related('wall_posts')
posts = group.wall_posts.all()
或者
class Wall(models.Model):
TYPE = (
(0, 'User'),
(1, 'Group'),
(2, 'Event'),
)
source = IntegerField() #id of user/group/event
source_type = SmallIntegerField(choices=TYPE)
class WallPost(models.Model):
text = models.TextField()
wall = models.ForeignKey(Wall)
我会这样做的。