How to add custom input form to django admin for adding dynamic data?

Viewed 129

I'm building an eCommerce website with multiple product variants. So how can I modify the Django admin panel, so that I can add new variants and the corresponding image from the same product adding page?

Here is my model:

class Products(models.Model):
    title = models.CharField(max_length=200, blank=False, db_index=True)
    description = models.TextField(blank=True, null=True)
    category = models.ManyToManyField(Category, default=None)
    thumbnail = models.ImageField(blank = True, upload_to = 'products/thumbnails')
    tags = models.CharField(max_length=100, blank=True, null=True)
    slug = models.TextField(max_length=500 ,blank=True, help_text="Client side only")
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)

class Attribute(models.Model):
    name = models.CharField(max_length=50, unique=True)
    def __str__(self):
        return self.name

class Attribute_Value(models.Model):
    attribute = models.ForeignKey(Attribute, on_delete=models.CASCADE)
    value = models.CharField(max_length=100, unique=True)

    def __str__(self):
        return self.value

class Product_Option(models.Model):
    product = models.ForeignKey(Products, on_delete=models.CASCADE)
    options = models.ForeignKey(Attribute, on_delete=models.PROTECT)

class Product_Combinations(models.Model):
    product = models.ForeignKey(Products, on_delete=models.CASCADE)
    combination = models.CharField(max_length=100, blank=True, default="")
    price = models.DecimalField(max_digits = 10, decimal_places=2, default = 0)


class Images(models.Model):
    product = models.ForeignKey(Product_Combinations, on_delete=models.CASCADE)
    # title = models.CharField(max_length=200, blank=True)
    image = models.ImageField(blank=True, upload_to='products/images')

Example- Attribute: size, color, material, height Attribute value: 3inch, 5inch, red, blue, wood, metal

Product 1: Product_Option- size, material

Product_Combinations-

       combination: 3 inch | wood
       price: 100

       combination: 5 inch | wood
       price: 200

       combination: 3 inch | metal
       price: 150

       combination: 5 inch | metal
       price: 250

Product 2: Product_Option- height, color

Product_Combinations-

       combination: 3 inch | red
       price: 100

       combination: 5 inch | red
       price: 200

       combination: 3 inch | blue
       price: 150

       combination: 5 inch | blue
       price: 250

I want to save the data like this from the admin panel directly. And all combinations will have their own different images.

If I choose attributes and attribute values, then Django should create all combinations dynamically like WordPress woo-commerce.

0 Answers
Related