Unsupported lookup 'product_name' for CharField or join on the field not permitted. Django Python ORM

Question:

When I click on the provided link it should show the products related to the particular shop but I get an error

Unsupported lookup ‘product_name’ for CharField or join on the field not permitted.

Please provide suggestions on how to resolve this.

product_name is the foreign key for ProductDetails and shop is the foreign key for Products

This is the template:

<a href="{{shop.shop_slug}}"><strong>{{shop.shop_location}}</strong></a>

This is views.py:

    # first check to see if the url is in categories.

    categories = [c.shop_slug for c in Shop.objects.all()]
    if single_slug in categories:
        matching_series = Product.objects.filter(shop_name__shop_slug=single_slug)
        series_urls = {}

        for m in matching_series.all():
            part_one = Product.objects.filter(product_name__product_name=m.product_name)
            series_urls[m] = part_one

        return render(request=request,
                      template_name='products/shop_products.html',
                      context={"product_name": matching_series, "part_ones": series_urls})
    
    products = [t.product_slug for t in Product.objects.all()]
    if single_slug in products:
        return HttpResponse(f"{single_slug} is a product")

    return HttpResponse(f"{single_slug} does not correspond")

This is models.py:

from django.db import models

# Create your models here.

# For shop in the beginning


# class ShopType(models.Model):
#     shop_type = models.CharField(max_length=50)
#     shop_type_description = models.TextField()

#     def __str__(self):
#         return self.shop_type


class Shop(models.Model):
    shop_name = models.CharField(max_length=50)
    shop_location = models.CharField(max_length=100)
    shop_opening_time = models.CharField(max_length=10)
    shop_slug = models.CharField(max_length = 20)
    # shop_type = models.ForeignKey(
    #     ShopType, default=1, on_delete=models.SET_DEFAULT)
    shop_image = models.ImageField(upload_to='products', null=True, blank=True)
    shop_owner = models.CharField(max_length=100)
    shop_description = models.TextField()
    shop_added_date = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.shop_name


class Product(models.Model):
    product_name = models.CharField(max_length=100)
    product_description = models.TextField()
    product_image = models.ImageField(
        upload_to='products', null=True, blank=True)
    product_price = models.DecimalField(max_digits=10, decimal_places=2)
    product_slug = models.CharField(max_length = 20)
    product_added_date = models.DateTimeField(auto_now=True)
    shop_name = models.ForeignKey(
        Shop, default=1, on_delete=models.SET_DEFAULT)

    def __str__(self):
        return self.product_name


class ProductDetails(models.Model):
    product_name = models.ForeignKey(
        Product, default=1, on_delete=models.SET_DEFAULT)
    test_something = models.CharField(max_length=20)

    def __str__(self):
        return self.test_something

Asked By: Alter Ego

||

Answers:

product_name is a CharField on the Product Model and therefore the double underscores (which are used to reference a Foreign Key Model’s attribute) are not required if you are creating a queryset on the Product Model itself. This line will cause an error:

part_one = Product.objects.filter(product_name__product_name=m.product_name).

Try instead:

part_one = Product.objects.filter(product_name=m.product_name)

Answered By: Nick Solly
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.