1

I am having a problem for some days but never get any solution, I tried different approach but not success. I want to pass multiple queryset in the same view or path.

@api_view(['GET'])
def getProduct(request, pk):
    product = Product.objects.get(_id=pk)
    related = Product.objects.filter(category=product.category).exclude(_id=pk).order_by('?')[:4]
    print(related)
    serializer = ProductSerializer(product, many=False)
    return Response(serializer.data)
    

I want to pass related product queryset which is related here in the same view. In the first section I will show product details which is working and in the next section I want to show related but now How can I apply that??

I got some solution which told me to go for serializers and change there. But I don't think so because I tried there but how it is possible. So anyone have any better solution. my related object has the same serializer which is productserializer. but eventually it is related product.

#this is my serializer class

class ProductSerializer(serializers.ModelSerializer):
    user = serializers.SerializerMethodField(read_only=True)
    class Meta:
        model = Product
        fields = '__all__'

    def get_user(self, obj):
        user = obj.user
        serializer = VendorSerializer(user, many=False)
        return serializer.data

#this is my product model

class Product(models.Model):

    brand_choices = [

        ('TVS', 'TVS'),
        ('Hero', 'Hero'),
        ('Yamaha', 'TVS'),
        ('Minister', 'Minister'),
        ('Walton', 'Walton'),
        ('Suzuki', 'Suzuki'),

    ]

    user = models.ForeignKey(Vendor, on_delete=models.CASCADE)
    name = models.CharField(max_length=220)
    image = models.ImageField(null=True, blank=True)
    brand = models.CharField(max_length=220, null=True, blank=True, choices=brand_choices)
    category = models.ForeignKey(Subcategory, on_delete=models.CASCADE)
    description = models.TextField(max_length=10000)
    rating = models.DecimalField(max_digits=7, decimal_places=2, blank=True, null=True)
    numReviews = models.IntegerField(null=True, blank=True, default=0)
    old_price = models.DecimalField(max_digits=11, decimal_places=2)
    discount = models.IntegerField(blank=True, null=True)
    price = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)
    countInStock = models.IntegerField(blank=True, null=True, default=0)
    createdAt = models.DateTimeField(auto_now_add=True)
    short_description = models.CharField(max_length=2000, blank=True, null=True)
    brandImage = models.ImageField(null=True, blank=True)
    isStorm = models.BooleanField(blank=True, null=True, default=False)
    _id = models.AutoField(primary_key=True, editable=False)
    

    def save(self, *args, **kwargs):
        self.price = Decimal(self.old_price * (100 - self.discount) / 100)
        return super(Product, self).save(*args, **kwargs)
    

    class Meta:
        ordering = ['-createdAt']


    def __str__(self):
        return self.name

and this is my updated serializer code

class ProductWithRelatedSerializer(ProductSerializer):
    related = serializers.SerializerMethodField(read_only=True)
    
    class Meta:
        model = Product
        fields = '__all__'

    def get_related(self, obj, pk):
        product = Product.objects.all()
        related = Product.objects.filter(category=
            product.category).exclude(_id=pk).order_by('?')[:4].select_related('Product')[:4]
        ps = ProductSerializer(related, many=True)
        return ps.data

this is my view

@api_view(['GET'])
def getProduct(request, pk):
    product = Product.objects.get(_id=pk)
    # related = Product.objects.filter(category=product.category).exclude(_id=pk).order_by('?')[:4]
    # print(related)
    serializer = ProductWithRelatedSerializer(product, many=False)
    return Response(serializer.data)
    

updated serializerr

class ProductWithRelatedSerializer(ProductSerializer):
    related = serializers.SerializerMethodField(read_only=True)
    
    class Meta:
        model = Product
        fields = '__all__'

    def get_related(self, obj):
        product = Product.objects.all()
        related = Product.objects.filter(category=
            product.category).exclude(_id=obj.pk).order_by('?')[:4].select_related('Product')[:4]
        ps = ProductSerializer(related, many=True)
        return ps.data

#product details page I used react can you please check and help me

import React, { useState, useEffect } from 'react'
import { Link } from 'react-router-dom'
import { Row, Col, Image, ListGroup, Form, Button, Card, Modal } from 'react-bootstrap'
import Rating from '../components/Rating'
import Loader from '../components/Loader'
import Message from '../components/Message'
import products from '../products'
import { useDispatch, useSelector } from 'react-redux'
import { listProductDetails } from '../actions/productActions'
import { productDetailsReducer } from '../reducers/productReducers'
import {InlineShareButtons} from 'sharethis-reactjs';
import {InlineFollowButtons} from 'sharethis-reactjs';
import { addToCart, removeFromCart } from '../actions/cartActions'
import { LinkContainer } from 'react-router-bootstrap'
import { Container} from 'react-floating-action-button'


function ProductScreen({ match, history }) {
    
    const cart = useSelector(state => state.cart)
    const { cartItems } = cart

    const removeFromCartHandler = (id) => {
        dispatch(removeFromCart(id))
    }

    const checkoutHandler = () => {
        history.push('/login?redirect=shipping')
    }

    const [show, setShow] = useState(false);
  
    const handleClose = () => setShow(false);

    const handleShow = () => setShow(true);

    const [qty, setQty] = useState(1)

    const dispatch = useDispatch()

    const productDetails = useSelector(state => state.productDetails)
    const {loading, error, product, related} = productDetails

    useEffect(() => {
        dispatch(listProductDetails(match.params.id))
    }, [dispatch, match])

    const addToCartHandler = () => {
        history.push(`/cart/${match.params.id}?qty=${qty}`)
    }
    
    return (
        <div>
            <Container>
             
                <Button
                    tooltip="Cart"
                    icon=""
                    rotate={true}
                    onClick={handleShow}
                    className="btn btn-primary btn-circle btn-xl"><i className="fas fa-shopping-cart" style={{fontSize:23}}><sup className='shanto'>{cartItems.length}</sup></i></Button>
            </Container>

            <Link to='/' className='btn btn-light my-3'><strong>Go Back</strong></Link>
            {loading ?
                <Loader />
                : error
                    ? <Message variant='danger'>{error}</Message>
                :(
                            <Row>
                                <Col md={6}>
                                    
                                    <Image src={product.image} alt={product.name} fluid />
                                </Col>
                                
                               
                            
                                <Col md={3}>
                                    <ListGroup variant="flush">
                                        <ListGroup.Item>
                                            <h3>{product.name}</h3>
                                        </ListGroup.Item>

                                        <ListGroup.Item>
                                            <h3>{related.id}</h3>
                                        </ListGroup.Item>


                                        <ListGroup.Item>
                                            Old Price: <strong className="tk">৳</strong><del> {product.old_price} </del>
                                        </ListGroup.Item>

                                        <ListGroup.Item >
                                            Discount: <b>{product.discount}%</b>
                                        </ListGroup.Item>

                                        <ListGroup.Item>
                                            Price: <strong className="tk">৳</strong> {product.price}
                                        </ListGroup.Item>


                                        <ListGroup.Item style={{color: 'red'}}>
                                            <p><b>{product.short_description}</b></p> 
                                        </ListGroup.Item>


 

1 Answer 1

2

You can implement an ProductWithRelatedSerializer that makes use of the ProductSerializer to serialize products. We thus subclass from the ProductSerializer:

class ProductWithRelatedSerializer(ProductSerializer):
    related = serializers.SerializerMethodField(read_only=True)

    def get_related(self, obj):
        related = Product.objects.filter(
            category_id=obj.category_id
        ).exclude(_id=obj.pk).order_by('?').select_related('user')[:4]
        ps = ProductSerializer(related, many=True)
        return ps.data

The .select_related(…) [Django-doc] is not necessary, but will boost the get_user method of the ProductSerializer for the related products.

This will thus return a response that looks like:

{
    /* … */
    "user": {
        …
    },
    related: [
         { "user": { … } },
         { "user": { … } },
         { "user": { … } },
         { "user": { … } }
    ]
}
Sign up to request clarification or add additional context in comments.

20 Comments

Then How will I view it in the frontend??
data.related is a list of serialized products. It thus adds an extra field related to the "outer" product, and you can enumerate over the list to render the related products, just like you render the selected product.
I mean what will be my view file and in this serializer it is saying product and pk undefined so I took a query product = Product.objects.all() and then call the product object and for pk I pass it as a parameter but it is saying one positional argument is missing
@SohanurRahmanShanto16209570: you replace serializer = ProductSerializer(product, many=False) in your view with serializer = ProductWithRelatedSerializer(product, many=False)
brother thank you so much for your effort. But it is giving me error like product not found and positional argument pk is missing
|

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.