如何为代理模型指定特定的相关名称

时间:2018-12-26 09:30:43

标签: django django-models

考虑以下模型:

class Engine(models.Model):
    Name = models.CharField(max_length=50, unique=True)

class Port(models.Model):
    Engine = models.ForeignKey(Engine, on_delete=models.CASCADE,
                        related_name='Ports')
    Name = models.CharField(max_length=150)
    Type_IO = models.BooleanField(choices=((True, 'IN'), (False,'OUT')))

class Port_IO_Manager(models.Manager):
    def __init__(self, inout):
        super().__init__()
        self.inout = inout
    def get_queryset(self):
        return super().get_queryset().filter(Type_IO=self.inout)

class Port_IN(Port):
    objects = Port_IO_Manager(True)
    class Meta(Port.Meta):
        proxy = True

class Port_OUT(Port):
    objects = Port_IO_Manager(False)
    class Meta(Port.Meta):
        proxy = True

如何定义相关名称Ports_INPorts_OUT,这样我可以使用以下内容:

Engine.objects.filter(Ports_IN__Name__contains='TR')

0 个答案:

没有答案