在另一个 TabularInline 模型中将图片库添加为 TabularInline

问题描述 投票:0回答:1

我想创建具有一些内联查询的产品模型,就像在每个产品中您可以添加不同的属性,如颜色、尺寸、品牌......并且每个变体可以有特定的图像库我使用两个模型库作为变体和变体的表格内联作为产品的 TabularInline 但它没有向我显示图像库作为 tabularinline 的变化,这是我的代码:

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

管理员.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'

我使用了 TabularInline 但它不起作用。 有人帮助我在这种情况下我应该怎么做才能为每个产品的每个变体建立一个图像库?

django variables django-models django-forms e-commerce
1个回答
0
投票

经过大量搜索,我找到了解决这个问题的方法,解决这个问题最好的方法是嵌套内联包:

pip install django-nested-inline
© www.soinside.com 2019 - 2024. All rights reserved.