如何从不同的模型获取所有数据?

时间:2014-01-28 13:09:22

标签: django django-models

我有模特:

class News(models.Model):
    title = ...
    content = ...
    creationdate = models.DateTimeField(default=datetime.now)
    [etc]

class Model_A(models.Model):
    name = ...
    info = ...
    news = models.ManyToManyfield(News)
    [etc]

class Model_B(models.Model):
    field_b = ...
    info = ...
    news = models.ManyToManyfield(News)
    [etc]

我想从model_a和model_b获取新闻,按createdate排序。怎么做?

1 个答案:

答案 0 :(得分:0)

假设您有model_a_inst作为Model_A的对象之一

model_a_inst.news.all().order_by('creationdate')