在筛选查询中访问父模型类字段

时间:2017-01-23 02:50:30

标签: python django django-models

是否可以通过过滤查询访问父模型中的字段?

例如,如果我有以下模型结构:

class Space(models.Model):
    name = models.CharField(max_length=500)
    ident = models.CharField(max_length=4)

    # Meta
    class Meta:
        abstract = True

class Airwaves(Space):
    @cached_property
    def stations(self):
        name_substr = self.name
        if " " in name_substr:
            name_substr = name_substr[:name_substr.index(" ")]

        return Station.objects.filter(Q(ident__contains=self.ident) |
                                          Q(name__contains=self.name) |
                                          Q(name__contains=name_substr))

class Radio(models.Model):
    # The fields I would like to access from Airwaves
    ident = models.CharField(max_length=4) 
    name = models.CharField(max_length=500)

    # Meta
    class Meta:
        abstract = True

class Station(Radio):
    frequency = models.DecimalField(max_digits=8, decimal_places=2)

使用现有代码,我收到错误Cannot resolve keyword 'ident' into field. Choices are: frequency, id

如何在ident中访问过滤查询的nameAirwaves字段?

0 个答案:

没有答案