1

hi i have 3 model name product, category, and categoryassin i want get 10 row from product object where category title is develop . i did like this but it not work and i get error

class 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)

err:

Related Field got invalid lookup: title

how can in fix it?

this is my models

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')

and this is related serilizers

 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',
                                    ]

1 Answer 1

2

Add this in you view:

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

obj = Product.objects.filter(product_id__in=prod_ids)
Sign up to request clarification or add additional context in comments.

2 Comments

i have error Related Field got invalid lookup: title
Add this in your view prod_ids = CategoryAssin.objects.filter(cat_id__title='develop').values_list('product', flat=True) obj = Product.objects.filter(product_id__in=prod_ids)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.