【问题标题】:How to add conditional fields to ModelAdmin in Django?如何在 Django 中向 ModelAdmin 添加条件字段?
【发布时间】:2016-05-07 03:49:03
【问题描述】:

我有模型类:

class Product(models.Model):
    category = models.ForeignKey(Category)
    name = models.CharField(max_length=90)
    ...
class Category(models.Model):
    name = models.CharField(max_length=90)
    description = models.CharField(max_length=2000)
    properies = models.ManyToManyField(Property)
    ...
#property type, ex: 'weight', 'length'
class Property(models.Model):
    ...
#value for every product
class PropertyValue(models.Model):
    product = models.ForeignKey(Product)
    property = models.ForeignKey(Property)
    ...

我需要自定义产品/添加/页面,设置 PropertyValue 表单取决于所选类别。 我已经通过category_id在modeldmin类中获取了propertyValue列表的方法,但是如何在所选类别更改时在运行时调用它?在 django 中可以吗?

【问题讨论】:

  • 请添加管理类的相关部分

标签: python django django-admin


【解决方案1】:

你在运行时说的是什么意思。如果这些类别发生变化,则每次加载添加页面时都会出现新记录。

你上过表格课吗?种类:

class PropertyValueForm(forms.Form):
    product = forms.ModelChoiceField(queryset=Product.objects.all())
    property = forms.ModelChoiceField(queryset=Property.objects.all())

或者:

def getProduct():
    # DO YOUR STUFF
    return product_list

class PropertyValueForm(forms.Form):
    product  = forms.ChoiceField(choices=get_my_choices())

【讨论】:

    猜你喜欢
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    相关资源
    最近更新 更多