I want to know can I write the fields of a product based on categories?
For example, if there is a product that is in the perfume category, it does not need the size of the screen that is in the category of electrical appliances.
I’m sorry, but your question isn’t really clear. What exactly are you trying to do that you can’t figure out?
We might be able to be more helpful if you provided more details and were more specific with what you’re trying to do. (If nothing else, the question in the body of your post doesn’t seem to match the title of the topic.)
Regardless, I’m going to say “yes”, because I’m guessing what you want to do is possible regardless of how I interpret the question.
this is models.py:
class Category(models.Model):
name = models.CharField(max_length=200, verbose_name=_('category'))
def __str__(self):
return self.name
class Product(models.Model):
title = models.CharField(max_length=200, verbose_name=_('title'))
description = models.TextField(verbose_name=_('description'))
favorite = models.ManyToManyField(get_user_model(), related_name='post_favs', default=None, blank=True,
verbose_name=_('favorite'))
cover = models.ImageField(upload_to='cover_products/', verbose_name=_('cover'))
price = models.PositiveIntegerField(verbose_name=_('price'))
off = models.BooleanField(default=False, verbose_name=_('off'))
off_price = models.PositiveIntegerField(null=True, blank=True, verbose_name=_('off price'))
active = models.BooleanField(default=True, verbose_name=_('active'))
category = models.models.ForeignKey(Category, on_delete=models.CASCADE, related_name='categorise', verbose_name=_('category'))
# phone
screen_size = models.DecimalField(max_digits=4, decimal_places=2, null=True, blank=True)
# perfume
volume = models.PositiveIntegerField(null=True, blank=True, verbose_name=_('volume'))
I want to show the volume field to fill when the user selects the perfume category in the admin django and the phone screen_size field is not displayed, because we don’t need this field.
You’re probably not going to want to use the admin for this. My initial reaction would be that you’re going to be better off creating your own form and views here.
You might be able to write some JavaScript to be added to your admin page to perform these screen manipulations, but I don’t know off-hand what that might look like.
(What my more basic reaction is that this probably isn’t the best structure for modelling this type of data. I’d probably be looking at structuring your models differently, depending upon how many categories you may end up with and the number of different category-based fields involved.)
Thank you. And what method do you suggest?