我有這些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