我有这些Models
,我希望能够从前两个选择。如何从Django的两个不同表中选择?
class Comments(models.Model):
post_id = models.ForeignKey('Posts')
content = models.CharField(max_length=480)
time_created = models.DateTimeField()
class Posts(models.Model):
user_id = models.ForeignKey('Users')
content = models.CharField(max_length=480)
time_created = models.DateTimeField()
class Users(models.Model):
email = models.EmailField()
name = models.CharField(max_length=60)
time_created = models.DateTimeField()
我希望能够选择的职位和他们的意见,并让他们通过datetime
排序,以便Posts
和Comments
会显示他们当混合。 我认为Twitter与他们的Tweets
和Retweets
做同样的事情。
您可能想查看[多对多关系](https://docs.djangoproject.com/en/1.7/topics/db/examples/many_to_many/)。 – Celeo 2014-09-23 17:29:02
@Celeo:我不明白多对多的关系在这里可以满足,因为'Post'不能由许多'Users'制作,同样一个'Comment'属于许多帖子。你能发出更多的光线吗? – Yax 2014-09-23 18:26:23
我应该链接[多对一](https://docs.djangoproject.com/en/1.7/ref/models/fields/#foreignkey)。 – Celeo 2014-09-23 18:39:29