2017-08-03 50 views
-1

我有这样的模型结构如下Django的模型嵌套串行

class BaseProduct: 
    id = models.CharField(max_length=15) 
    name = models.CharField(max_length=20) 

class Product 
    base_product = ForeigKey(BaseProduct) 
    name = models.CharField(max_length=20) 

class Condition: 
    category = models.ForeignKey(Product, related_name='allowed_product') 
    check = models.IntegerField(default=0) 
    allow = models.PositiveSmallIntegerField(default=1) 

查询:

Product.objects.filter(condition__allow=1, condition__check=1) 

我想格式化类似下面 基础产品和产品的该列表内基于允许并检查过滤器

[ 
    { 
     "name": "BaseProduct 1", 
     "products": [ 
      { 

       "name": "TV", 

      }, {}, .... 

     ] 
    }, 
........ 
] 

回答

1

试试吧,如果你使用Django的REST框架

from rest_framework import serializers 
from rest_framework.fields import empty 
from django.utils.functional import cached_property 


class ProductSerializer(serializers.ModelSerializer): 

    class Meta: 
     model = Product 
     fields = ('name') 


class BaseProductSerializer(serializers.ModelSerializer): 
     products = serializers.SerializerMethodField() 

    class Meta: 
     model = BaseProduct 
     fields = ('name', 'products') 

    def __init__(self, instance=None, data=empty, **kwargs): 
     self._condition_allow = kwargs.pop('condition_allow', 1) 
     super(BaseProductSerializer, self).__init__(instance=None, data=empty, **kwargs) 

    @cached_property 
    def _request_data(self): 
     request = self.context.get('request') 
     # if POST 
     # return request.data if request else {} 
     # if GET params 
     return request.query_params if request else {} 

    @cached_property 
    def _condition(self): 
     return self._request_data.get('CONDITION_PARAM_NAME') 

    def get_products(self, obj): 
     qs = obj.product_set.filter(condition__allow=self._condition_allow, condition__check=1) 
     serializer = ProductSerializer(qs, many=True) 
     #        ^^^^^ 
     return serializer.data 

鉴于

serialiser(qs, condition_allow=5) 
+0

如何通过序列化程序中的条件检查? condition__check = 1从视图(值1可以改变)? –

+0

@RoshanA使用请求编辑示例参数,DRF的所有信息 –

+0

嗨,谢谢。条件参数不是来自请求。我必须根据查询参数处理内部视图并找到id。如何直接将该ID传入串行器以获取数据。 –

1

改变你的模型具有related_name为foreignkeys有反向关系:

class BaseProduct: 
    id = models.CharField(max_length=15) 
    name = models.CharField(max_length=20) 

class Product 
    base_product = ForeigKey(BaseProduct, related_name='products') 
    name = models.CharField(max_length=20) 

class Condition: 
    category = models.ForeignKey(Product, related_name='conditions') 
    check = models.IntegerField(default=0) 
    allow = models.PositiveSmallIntegerField(default=1) 

所以,现在你可以在你的序列化程序中使用它:

class BaseProductSerializer: 
    class Meta: 
    model = BaseProduct 
    fields = ('name', 'products',) 

class ProductSerializer: 
    class Meta: 
    model = Product 
    fields = ('conditions',) 

class ConditionSerializer: 
    class Meta: 
    model = Condition 
    fields = '__all__' 

最后,在你的意见,改变这种:

Product.objects.filter(condition__allow=1, condition__check=1) 

到这一点:

BaseProduct.objects.filter(products__conditions__allow=1, products__conditions__allow=1) 

并希望,这应该会在你要求的格式JSON数据。

+0

内连接工作的方式是错误的。这个查询不会给出正确的结果。 –