从django模型中选择列出的字段

时间:2011-10-20 11:03:19

标签: django django-models

我已经完成了这个https://docs.djangoproject.com/en/1.1/ref/models/querysets/#values-fields 但我的要求是不同的。

我有一个定制经理的商店模型:

class VegManager(models.Manager):
    def get_query_set(self):
        return super(VegManager, self).get_query_set().filter(plus=True)

class FruitsManager(models.Manager):
    def get_query_set(self):
        return super(FruitsManager, self).get_query_set().filter(plus=True)


class SweetsManager(models.Manager):
    def get_query_set(self):
        return super(SweetsManager, self).get_query_set().filter(plus=True)


    class Store(models.Model):
        vegetable = models.BooleanField()
        veg1_price = models.DecimalField(max_digits=6, decimal_places=2, null=True, blank=True)
        veg2_price = models.DecimalField(max_digits=6, decimal_places=2, null=True, blank=True)
        veg3_price = models.DecimalField(max_digits=6, decimal_places=2, null=True, blank=True)
        fruits = models.BoolearnField()
        fruit1_price = models.DecimalField(max_digits=6, decimal_places=2, null=True, blank=True)
        fruit2_price = models.DecimalField(max_digits=6, decimal_places=2, null=True, blank=True)
        fruit3_price = models.DecimalField(max_digits=6, decimal_places=2, null=True, blank=True)
        sweets = models.BooleanField()
        sweet1_price = models.DecimalField(max_digits=6, decimal_places=2, null=True, blank=True)
        sweet2_price = models.DecimalField(max_digits=6, decimal_places=2, null=True, blank=True)
        sweet3_price = models.DecimalField(max_digits=6, decimal_places=2, null=True, blank=True)
        objects = models.Manager()
        v = VegManager()
        f = FruitsManager()
        s = SweetsManager()

    class Veg(Store):
        class Meta:
            proxy = True

    class  Froots(Store):
        class Meta:
            proxy = True

    class Sweet(Store):
        class Meta:
            proxy = True

如果我做sweets.v.all()并且它只给我蔬菜的价格吗?而不是获得糖果和水果的所有价格数据?

谢谢!

2 个答案:

答案 0 :(得分:0)

您不应该通过vegetablefruitssweets布尔字段而不是plus过滤您的经理,这是我甚至没有看到的字段你的模特?

class VegManager(models.Manager):
    def get_query_set(self):
        return super(VegManager, self).get_query_set().filter(vegetables=True)

class FruitsManager(models.Manager):
    def get_query_set(self):
        return super(FruitsManager, self).get_query_set().filter(fruits=True)

class SweetsManager(models.Manager):
    def get_query_set(self):
        return super(SweetsManager, self).get_query_set().filter(sweets=True)

答案 1 :(得分:0)