At this moment I am creating a project to learn how to use django. In this project there are three models:
And what I'm trying to achieve is to dynamically choose the corresponding subcategory after selecting the category, and thus be able to add the product.
I tried to use smart_selects without results, since it is not compatible with python 3.7
I tried to filter the list of subcategories using the selected category field as a filter, without success, I think I am not formulating the code well, so that it takes the value of the category field to filter the list of subcategories dynamically.
This is my code:
models.py
"""Z"""
from django.db import models
class Category(models.Model):
"""Z"""
category_name = models.CharField("Name", max_length=50)
class Meta:
ordering = ('pk',)
verbose_name = "Category"
verbose_name_plural = "Categories"
def __str__(self):
"""Z"""
return self.category_name
class Subcategory(models.Model):
"""Z"""
subcategory_name = models.CharField("Name", max_length=50)
subcategory_parent = models.ForeignKey(
Category, verbose_name="Parent", on_delete=models.CASCADE)
class Meta:
ordering = ('pk',)
verbose_name = "Subcategory"
verbose_name_plural = "Subcategories"
def __str__(self):
return self.subcategory_name
class Product(models.Model):
"""Z"""
category = models.ForeignKey(
Category, verbose_name="Category", on_delete=models.SET_NULL, null=True)
subcategory = models.ForeignKey(
Subcategory, verbose_name="Subcategory", on_delete=models.SET_NULL, null=True)
product_name = models.CharField("Name", max_length=50, unique=True)
class Meta:
ordering = ('pk',)
verbose_name = "Product"
verbose_name_plural = "Products"
def __str__(self):
"""Z"""
return self.product_name
admin.py
"""Z"""
from django.contrib import admin
from .models import Category, Subcategory, Product
class SubcategoryInline(admin.StackedInline):
"""Z"""
model = Subcategory
extra = 1
@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
"""Z"""
inlines = (SubcategoryInline,)
@admin.register(Subcategory)
class SubcategoryAdmin(admin.ModelAdmin):
"""Z"""
@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
"""Z"""
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "subcategory":
kwargs["queryset"] = Subcategory.objects.filter(subcategory_parent=request.category)
return super().formfield_for_foreignkey(db_field, request, **kwargs)
And this is the error that is shown when I'm trying to use it:
AttributeError at /admin/prototype/product/add/
'WSGIRequest' object has no attribute 'category'
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/prototype/product/add/
Django Version: 2.0.9
Exception Type: AttributeError
Exception Value:
'WSGIRequest' object has no attribute 'category'
Exception Location: C:\asp\pilgrim\prototype\admin.py in formfield_for_foreignkey, line 30
Python Executable: C:\Program Files\Python37\python.exe
Python Version: 3.7.0
Python Path:
['C:\\asp\\pilgrim',
'C:\\Program Files\\Python37\\python37.zip',
'C:\\Program Files\\Python37\\DLLs',
'C:\\Program Files\\Python37\\lib',
'C:\\Program Files\\Python37',
'C:\\Users\\lisandro\\AppData\\Roaming\\Python\\Python37\\site-packages',
'C:\\Program Files\\Python37\\lib\\site-packages']
Server time: Tue, 1 Jan 2019 11:48:18 -0400
You want to pass a filtered queryset to a custom form that you use in the admin (or in a particular view/template, if you're trying to do this in the real frontend). See the answer here: https://stackoverflow.com/a/291968/11295291