Show the child Query in admin-panel of Django

23 Views Asked by At

I have different variation in attribute model by tabularinline:

class Attribute(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(max_length=250, unique=True)
    description = models.TextField(null=True, blank=True)


    def __str__(self):
        return self.title
class Variations(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(max_length=250, unique=True)
    description = models.TextField(null=True, blank=True)
    image = models.ImageField(upload_to='variations', null=True, blank=True)
    attribute = models.ForeignKey(Attribute, null=True, on_delete=models.SET_NULL, related_name='variation')


    def __str__(self):
        return self.title

now I made a relationship Product model with Attribute:

class Product(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(max_length=250, unique=True)
    description = RichTextUploadingField(null=True)
    short_description = RichTextUploadingField(null=True)
    regular_price = models.IntegerField()
    sale_price = models.IntegerField(null=True, blank=True)
    sku = models.CharField(max_length=200, null=True, blank=True)
    dimenstions = models.CharField(max_length=200, null=True, blank=True)
    stock_quantity = models.IntegerField(default=0)
    category = models.ManyToManyField(Category, related_name='category')
    tag = models.ManyToManyField(Tags, related_name='tag')
    attributes = models.ManyToManyField(Attribute, through='ProductVariation', related_name='products')
    image = models.ImageField(upload_to='shop/image', null=True, blank=True)
    # tax_class = pass
    # shiping_class = pass
    # related_product = pass
    # variation = pass


    def __str__(self):
        return self.title
    
    def thumbnail_tag(self):
        return format_html(
            "<img width=100 height=75 style='border-radius: 5px;' src='{}'>".format(self.image.url))

    thumbnail_tag.short_description = "Thumbnail"


class ProductVariation(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    attribute = models.ForeignKey(Attribute, on_delete=models.CASCADE)


    def __str__(self):
        return f"{self.product} - {self.attribute_value}"

Now I want when users choose attribute 1 to show them in another field all the variations of that attribute and do this for different attributes with different variations. For example when I want to add a new product in admin-panel if I choose attribute of color show me another field of I color that I have in this attribute

0

There are 0 best solutions below