I have an entity of Products, category and sub-category. A category can have many products and also a category can have many sub-categories. For example a product called Iphone-4 can fall on smart phone subcategories of Electronics & Gadgets Category. Similiary samsung product can fall on same smart-phone sub-categories of Electronics&Gadgets Category. How could i show this relation effectively?
Here is what i did
class Category(models.Model):
name = models.CharField(max_length=80, null=True)
parent = models.ForeignKey('self', blank=True, null=True, related_name="children")
class Meta:
unique_together = ('parent',)
verbose_name = 'Category'
verbose_name_plural = 'Categories'
class Product(models.Model):
token = models.CharField(default=token_generator, max_length=20, unique=True, editable=False)
category = models.ForeignKey(Category)
company = models.ForeignKey(Company, related_name="company_product")
name = models.CharField(max_length=200, null=True)
model = models.CharField(max_length=100, blank=True, null=True)
specification = models.TextField(null=True, blank=True)
price = models.PositiveIntegerField(default=0)
admin.py
class ProductAdmin(admin.ModelAdmin):
list_select_related = ('category', 'company',)
class Meta:
model = Product
class CategoryAdmin(admin.ModelAdmin):
list_select_related = ('parent',)
list_display = ('name', 'parent', )
class Meta:
model = Category
This way in my admin, the categories and sub-categories are shown so vaguely. Category Electronics & Gadgets is shown multiple times. It is shown as per the number of sub-category that falls under this category.
Is this expected behavior or has to handle this other effective way?
Here is the code snippet to list nested Category and Sub-Category inside the Admin Panel. Please register the Category model in the admin.py of your application first.
# Category Model.
class Category(models.Model):
name = models.CharField(blank=False, max_length=200)
slug = models.SlugField(null=False)
parent = models.ForeignKey('self',blank=True, null=True ,related_name='children', on_delete=models.SET_NULL)
class Meta:
db_table = "dj_categories"
# Add verbose name
verbose_name = 'Category'
verbose_name_plural = "Categories"
unique_together = ('slug', 'parent',)
def __str__(self):
return self.name
Related
models.py
class FarmerAdvisory(BaseModel):
id = models.AutoField(db_column='id', primary_key=True)
title = models.CharField(db_column='title', max_length=200, null=True, blank=True)
description = models.CharField(db_column='description', max_length=750, null=True, blank=True)
farmer_id = models.ForeignKey(Farmer, on_delete=models.CASCADE, null=True, db_column='farmer_id')
class Meta:
verbose_name = 'Farmer Advisory'
verbose_name_plural = 'Farmer Advisories'
managed = True
db_table = 'farmer_advisory'
class ReplyFarmerAdvisory(BaseModel):
reply = models.CharField(db_column='reply', max_length=750, null=True, blank=True)
label = models.CharField(db_column='label', max_length=100, null=True, blank=True)
farmer_advisory_id = models.ForeignKey(FarmerAdvisory, on_delete=models.CASCADE, db_column='farmer_advisory_id')
objects = models.Manager()
class Meta:
verbose_name = 'Reply Farmer Advisory'
verbose_name_plural = 'Reply Farmer Advisories'
managed = True
db_table = 'reply_farmer_advisory'
class AdvisoryMedia(models.Model):
id = models.AutoField(db_column='id', primary_key=True)
farmer_advisory_id = models.ForeignKey(FarmerAdvisory, on_delete=models.CASCADE,
db_column='farmer_advisory_id',
null=True, blank=True)
reply_farmer_advisory_id = models.ForeignKey(ReplyFarmerAdvisory, on_delete=models.CASCADE,
db_column='reply_farmer_advisory_id', null=True, blank=True)
farmer_media_file = models.CharField(db_column='farmer_media_file', max_length=50, null=True, blank=True)
is_active = models.BooleanField(db_column='is_active', null=False, default=True)
reply_media_file = models.FileField(upload_to=content_file_name, null=True, blank=True,
db_column='reply_media_file')
class Meta:
verbose_name = 'Advisory Media'
verbose_name_plural = 'Advisories Media'
managed = True
db_table = 'advisory_media'
admin.py
class AdvisoryMediaInline(NestedStackedInline):
model = AdvisoryMedia
extra = 0
class ReplyFarmerAdvisoryInline(NestedStackedInline):
model = ReplyFarmerAdvisory
extra = 1
inlines = [AdvisoryMediaInline]
class FarmerAdvisoryAdmin(NestedModelAdmin):
inlines = [ReplyFarmerAdvisoryInline]
I want to show the fields with respect to the farmer advisory. But when i am adding fk_name = 'farmer_advisory_id' inside AdvisoryMediaInline, i am getting error stating
ValueError: fk_name 'farmer_advisory_id' is not a ForeignKey to
'farmer.ReplyFarmerAdvisory'.
But i want to show the farmer advisory related fields which is not coming by itself.
I think what is happening is the model is looking for values with respect to the to foreign key reply advisory media instead of farmer advisory media.
Please let me know if my question is understandable, if yes please guide me through it.
My models, category and product. Each product has a category field, which is linked through ForeignKey.
class Category(models.Model):
name = models.CharField(max_length=50, unique=True)
description = models.TextField()
image = models.ImageField(upload_to='category', blank=True)
class Meta:
verbose_name = 'category'
verbose_name_plural = 'categories'
def __str__(self):
return self.name
class Product(models.Model):
name = models.CharField(max_length=50, unique=True)
description = models.TextField()
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='category')
price = models.DecimalField(max_digits=10, decimal_places=2)
image = models.ImageField(upload_to='product', blank=True)
stock = models.PositiveIntegerField()
available = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now=True)
class Meta:
verbose_name = 'product'
verbose_name_plural = 'products'
def __str__(self):
return self.name
And views, a product category can be selected and when I click on a category, I want the product to appear only in that category
class CategoriesList(LoginRequiredMixin, ListView):
login_url = 'login/'
template_name = 'index.html'
model = Category
class ProductsList(ListView):
template_name = 'products.html'
model = Product
def get_queryset(self):
return super().get_queryset().filter(category=category_id)
at the moment I try to get recipes from my API. I have a Database with two tables one is with recipes and their ids but without the ingredients, the other table contains the ingredients and also the recipe id. Now I cant find a way that the API "combines" those. Maybe its because I added in my ingredient model to the recipe id the related name, but I had to do this because otherwise, this error occurred:
ERRORS:
recipes.Ingredients.recipeid: (fields.E303) Reverse query name for 'Ingredients.recipeid' clashes with field name 'Recipe.ingredients'.
HINT: Rename field 'Recipe.ingredients', or add/change a related_name argument to the definition for field 'Ingredients.recipeid'.
Models
from django.db import models
class Ingredients(models.Model):
ingredientid = models.AutoField(db_column='IngredientID', primary_key=True, blank=True)
recipeid = models.ForeignKey('Recipe', models.DO_NOTHING, db_column='recipeid', blank=True, null=True, related_name='+')
amount = models.CharField(blank=True, null=True, max_length=100)
unit = models.CharField(blank=True, null=True, max_length=100)
unit2 = models.CharField(blank=True, null=True, max_length=100)
ingredient = models.CharField(db_column='Ingredient', blank=True, null=True, max_length=255)
class Meta:
managed = False
db_table = 'Ingredients'
class Recipe(models.Model):
recipeid = models.AutoField(db_column='RecipeID', primary_key=True, blank=True) # Field name made lowercase.
title = models.CharField(db_column='Title', blank=True, null=True, max_length=255) # Field name made lowercase.
preperation = models.TextField(db_column='Preperation', blank=True, null=True) # Field name made lowercase.
images = models.CharField(db_column='Images', blank=True, null=True, max_length=255) # Field name made lowercase.
#ingredients = models.ManyToManyField(Ingredients)
ingredients = models.ManyToManyField(Ingredients, related_name='recipes')
class Meta:
managed = True
db_table = 'Recipes'
When there is no issue it has to be in the serializer or in the view.
Serializer
class IngredientsSerializer(serializers.ModelSerializer):
# ingredients = serializers.CharField(source='ingredients__ingredients')
class Meta:
model = Ingredients
fields = ['ingredient','recipeid']
class FullRecipeSerializer(serializers.ModelSerializer):
ingredients = IngredientsSerializer(many=True)
class Meta:
model = Recipe
fields = ['title','ingredients']
View
class FullRecipesView(generics.ListCreateAPIView):
serializer_class = FullRecipeSerializer
permission_classes = [
permissions.AllowAny
]
queryset = Recipe.objects.all()
This is at the moment my output
But I want e.g. the recipe with id 0 and all the ingredients which have also recipe id 0.
I really hope that you can help me. Thank you so much!
Rename ingredients to some other name in FullRecipeSerializer. It conflicts with ingredients in Recipe model. This should solve your issue. For example
class FullRecipeSerializer(serializers.ModelSerializer):
ingredients_recipe = IngredientsSerializer(many=True, source= 'ingredientid')
class Meta:
model = Recipe
fields = ['title','ingredients_recipe']
I want to make a model with products that will have a subcategory that will fall into its category.
Here is an example: outerwear (category) -> t-shirt (subcategory) -> White t-shirt with a pattern (product)
But I was faced with the problem that the name of the subcategory is not displayed in the product.
My models.py
class Global_category(models.Model):
Name = models.CharField(max_length=20, verbose_name='Имя')
slug = models.SlugField(max_length=20, verbose_name='Slug', unique=True)
class Meta:
ordering = ['Name']
verbose_name = 'Категория'
verbose_name_plural = 'Категории'
def __str__(self):
return self.Name
class SubCategory(models.Model):
Name = models.CharField(max_length=20, verbose_name='Имя подкатегории')
slug = models.SlugField(max_length=20, verbose_name='Slug', unique=True)
Global_category = models.ForeignKey(Global_category, on_delete=models.CASCADE, verbose_name='Имя Категории')
class Meta:
ordering = ['Name']
verbose_name = 'Подкатегория'
verbose_name_plural = 'Подкатегории'
class Product(models.Model):
Category = models.ForeignKey(SubCategory, on_delete=models.CASCADE, default=None)
I will be glad to any help
Just add it to your model SubCategory
def __str__(self):
return self.name
django.db.models.Model.str
I have a product model, category model and a product_category model for storing the product id to multiple categories.
models.py
class Product(models.Model):
title = models.CharField(max_length=255, blank=True)
slug = models.SlugField(_('Slug'), max_length=255, unique=False)
class Category(models.Model):
name = models.SlugField(_('Slug'), max_length=255, unique=False)
class ProductCategory(models.Model):
product = models.ForeignKey('catalogue.Product', verbose_name=_("Product"))
category = models.ForeignKey('catalogue.Category', verbose_name=_("Category"))
My serializers.py
class MyProductLinkCustomSerializer(ProductLinkSerializer):
class Meta(ProductLinkSerializer.Meta):
fields = ('url', 'id', 'product_id', 'title',)
views.py
class ProductListCustom(basic.ProductList):
def get_queryset(self):
category_id = self.request.query_params.get('category_id', None)
#filter products with this category_id, if someone provided category_id in url
return Product.objects.all()
serializer_class = MyProductLinkCustomSerializer
I want to list the products with category object for each product belongs to. Also I want to filter the products by the category_id it belongs.
Actually, I think you should consider: using models.ManyToManyField to link category and product.
class Product(models.Model):
title = models.CharField(max_length=255, blank=True)
slug = models.SlugField(_('Slug'), max_length=255, unique=False)
categories = models.ManyToManyField(Category, related_name='products')
Then, you can filter products like this:
Product.objects.filter(categories=category_id)
here is some useful example for ManyToManyField