在ORM Django中加入两个表

时间:2016-09-07 13:49:52

标签: django-models

人物,作者和歌手有三种模式。

class Person(models.Model):
    name = models.CharField(max_length=256, null=True)
    class Meta:
       db_table = "Person"

class Author(models.Model):
    person = models.ForeignKey(Person)
    rating = models.InegerField(null=True)
    class Meta:
       db_table = "Author"

class Singer(models.Model):
    person = models.ForeignKey(Person)
    rating = models.IntegerField(null=True)
    class Meta:
       db_table = "Singer"

我必须在ORM中进行作者和歌手的加入

MySQL就像 从Singer选择S. *作为S在S.person_id = P.person_id上加入作者A;

1 个答案:

答案 0 :(得分:0)

根据您的数据库模型,更简单的解决方案是

author_objects = Author.objects.filter(person=singer_object.person) # where singer_object

# now do something with the author_objects