如何在管理控制台中刷新内联表单?

时间:2019-12-16 14:12:39

标签: python django python-3.x django-admin

我有模型(类别,项目,AttributeCategory,AttributeitemValue),当我添加新项目时,在项目中我具有AttributeitemValueInline,然后我更改了项目中的类别,我想获得该项目包括类别的项目的可能属性值。

例如类别“笔记本电脑和平板电脑”具有(RAM和磁盘类型),类别“床和椅子”具有(宽度和高度),当我添加新项目并选择类别时,内联表格Attribute(该内联表格在Item中,)使只有属性值链接到类别。我添加了项目笔记本电脑的输入名称并选择类别,内联表单捕获了该值,并且只有RAM和Disk Type属性可以添加到项目中。对不起,我的英语不好。

如何从类别中动态获取属性?创建商品吗?

class AttributeItemValueInline(admin.TabularInline):
    model = AttributeItemValue


class AttributeItemValueAdmin(admin.ModelAdmin):
    pass

admin.site.register(AttributeItemValue, AttributeItemValueAdmin)

class ItemAdmin(admin.ModelAdmin):
    inlines = [
        AttributeItemValueInline,
    ]

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "category":
            kwargs["queryset"] = Category.objects.filter(parent__isnull = False)
        return super().formfield_for_foreignkey(db_field, request, **kwargs)

admin.site.register(Item, ItemAdmin)

models.py

class Category(models.Model):
    name = models.CharField(max_length = 255)
    slug = models.SlugField()
    parent = models.ForeignKey('self',on_delete = models.PROTECT, blank=True, null=True)

    def __str__(self):
        return self.name

    def __unicode__(self):
        return self.name

class Item(models.Model):
    name = models.CharField(max_length = 255)
    slug = models.SlugField()
    description = models.TextField()
    price = models.DecimalField(max_digits=2,decimal_places=2)
    category = models.ForeignKey('Category', related_name='categories', on_delete=models.CASCADE)

    def __str__(self):
        return self.name

    def __unicode__(self):
        return self.name

class Attribute(models.Model):
    name = models.CharField(max_length = 255)

    def __str__(self):
        return self.name

    def __unicode__(self):
        return self.name


class Unit(models.Model):
    name = models.CharField(max_length = 255)

    def __str__(self):
        return self.name

    def __unicode__(self):
        return self.name

class ImageItem(models.Model):
    picture = models.ImageField()
    item = models.ForeignKey('Item',on_delete=models.CASCADE)
    def __str__(self):
        return self.item.name + " picture"

    def __unicode__(self):
        return self.item.name + " picture"

class AttributeCategory(models.Model):
    attribute = models.ForeignKey("Attribute", on_delete=models.CASCADE)
    category = models.ForeignKey("Category", on_delete=models.CASCADE)

    def __str__(self):
        return self.category.name + " -> " + self.attribute.name

    def __unicode__(self):
        return self.category.name + " -> " + self.attribute.name

class AttributeItemValue(models.Model):
    attribute = models.ForeignKey('Attribute',on_delete=models.CASCADE)
    item = models.ForeignKey('Item',on_delete=models.CASCADE)
    numerical_value = models.DecimalField(max_digits=2,decimal_places=2)
    text_value = models.CharField(max_length = 255)
    unit = models.ForeignKey('Unit',on_delete=models.PROTECT)

    def __str__(self):
        return self.item.name + " -> " + self.attribute.name + " -> Value"


    def __unicode__(self):
        return self.item.name + " -> " + self.attribute.name + " -> Value"

class schema

0 个答案:

没有答案