I want to Create Product model which has some Inline query like in each product you can add different attribute like color, size, brand,... and each variation could has specefic image gallery I use two model gallery as TabularInline of Variation and Variation as TabularInline of Product But it didn't show me tha image gallery as tabularinline in variation, Here is my code:
productapp.models:
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)
price = models.IntegerField()
sku = models.CharField(max_length=200, null=True, blank=True)
dimenstions = models.CharField(max_length=200, null=True, blank=True)
category = models.ManyToManyField(Category, related_name='category')
tag = models.ManyToManyField(Tags, related_name='tag')
attribute = models.ManyToManyField(Attribute, related_name='attribute')
image = models.ImageField(upload_to='shop/image', null=True, blank=True)
def __str__(self):
return self.title
class Variant(models.Model):
title = models.CharField(max_length=200)
product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
variations = models.ForeignKey(Variations, on_delete=models.SET_NULL, null=True, blank=True)
regular_price = models.IntegerField()
sale_price = models.IntegerField(null=True, blank=True)
sku = models.CharField(max_length=200, null=True, blank=True)
stock_quantity = models.IntegerField(default=0)
image_id = models.IntegerField(blank=True, null=True, default=0)
def __str__(self):
return self.title
class Gallery(models.Model):
title = models.CharField(max_length=200)
image = models.ImageField(upload_to='shop/gallery', null=True, blank=True)
product = models.ForeignKey(Variant, null=True, on_delete=models.SET_NULL, related_name='gallery')
def __str__(self):
return self.title
Admin.py:
class GalleriesInline(admin.TabularInline):
model = Gallery
extra = 1
readonly_fields = ('pk',)
formfield_overrides = {
models.CharField: {'widget': TextInput(attrs={'size':'30'})},
}
class VariantInline(admin.TabularInline):
model = Variant
inlines = [GalleriesInline]
extra = 1
readonly_fields = ('image_tag',)
show_change_link = True
@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
form = ProductForm
inlines = [VariantInline]
prepopulated_fields = {"slug": ["title"]}
list_display = ('thumbnail_tag', 'title')
readonly_fields = ('image_preview',)
def image_preview(self, obj):
# ex. the name of column is "image"
if obj.image:
return mark_safe('<img src="{0}" width="150" height="150" style="object-fit:contain" />'.format(obj.image.url))
else:
return '(No image)'
image_preview.short_description = 'Preview'
I used TabularInline but it didn't work. Someone Help me in this situation what should I do to have an Image gallery for each variation of each product?
After A lot of searching, I found a way to solve this problem, The best thing that you can use for this problem is a nested inline package:
pip install django-nested-inline