django rest在对象过滤器中使用关系模型字段

时间:2018-04-12 20:50:37

标签: python django django-rest-framework

我有3个模型名称categorycategoryassinclass GetWebProductAPiView(APIView): def get(self,request): obj = Product.objects.filter(category__title = "develop") serializer = ProductSerializer(instance=obj,many=True,context={'request': request}) return Response(serializer.data) 我想从产品对象中获得10行,其中类别标题是开发的。 我喜欢这样,但它不起作用,我得到错误

Related Field got invalid lookup: title

错误:

class Product(models.Model):
    product_id = models.AutoField(primary_key=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE, db_index=True)
    title = models.CharField(max_length=200)
    video_length = models.CharField(max_length=20, null=True, blank=True)
    mini_description = models.CharField(max_length=1000, null=True, blank=True)
    full_description = models.TextField(null=True, blank=True)
    you_need = models.CharField(max_length=1000, null=True)
    you_learn = models.CharField(max_length=2000, null=True)
    price = models.CharField(max_length=50, null=True, blank=True)
    free = models.BooleanField(default=False)
    video_level = models.CharField(max_length=100, null=True, blank=True)
    created_date = models.DateTimeField(auto_now_add=True)
    updated_date = models.DateTimeField(auto_now=True)
    publish = models.BooleanField(default=False)
    draft = models.BooleanField(default=False)
    slug = models.SlugField(allow_unicode=True, null=True, blank=True)
    image = models.FileField(upload_to=upload_to_custom_p, null=True, blank=True)

class Category(models.Model):
    parent_id = models.IntegerField(null=True, blank=True)
    title = models.CharField(max_length=200)


class CategoryAssin(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE, to_field='product_id', related_name='category')
    cat_id = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='categoryid')

如何修复它?

这是我的模特

 class CategorySerializer(ModelSerializer):
        class Meta:
            model = Category
            fields = [
                'id',
                'parent_id',
                'title',
            ]
            read_only_fields = ['id']

  class CategoryAssinSeralizer(ModelSerializer):
            product = serializers.SerializerMethodField()

            def get_product(self, obj):
                return obj.product.title

            cat_name = serializers.SerializerMethodField()

            def get_cat_name(self, obj):
                return obj.cat_id.title

            class Meta:
                model = CategoryAssin
                fields = [
                    'cat_id',
                    'cat_name',
                    'product',
                ]
                read_only_fields = ['id']


        # product
    class ProductSerializer(ModelSerializer):
            product_ratings = ProductRatingsSerializer(many=True, read_only=True)
            product_discount = ProductDiscountControllSerializer(read_only=True)
            product_video = ProductWithoutVideoSerializer(many=True, read_only=True)
            author = serializers.SerializerMethodField()
            base64_image = serializers.SerializerMethodField(read_only=True, allow_null=True)

            def get_author(self, obj):
                return obj.author.first_name + ' ' + obj.author.last_name


            category = CategoryAssinSeralizer(many=True)


            url = main_page_post

            class Meta:
                model = Product
                fields = [
                    'product_id',
                    'url',
                    'author',
                    'title',
                    'mini_description',
                    'you_learn',
                    'you_need',
                    'full_description',
                    'price',
                    'free',
                    'video_level',
                    'video_length',
                    'created_date',
                    'updated_date',
                    'product_ratings',
                    'product_discount',
                    'product_video',
                    'image',
                    'slug',
                    'draft',
                    'publish',
                    'category',
                    'base64_image',
                ]
                read_only_fields = ['product_id',
                                    'created_date',
                                    'updated_date',
                                    'author',
                                    'base64_image',
                                    'product_ratings',
                                    'product_discount',
                                    ]

这是相关的血清化剂

p = zeros(1,256);
 m = zeros(1,12);

1 个答案:

答案 0 :(得分:2)

在您查看中添加此内容:

prod_ids = CategoryAssin.objects.filter(cat_id__title='develop').values_list('product', flat=True)

obj = Product.objects.filter(product_id__in=prod_ids)