Django - 管理操作表单,其中包含Verbose_names列表的ChoiceField

时间:2012-12-10 13:32:14

标签: django

我有一个模特:

class Inventory(models.Model):
    title = models.CharField(max_length=100, db_index=True)
    product_code = models.CharField(max_length=100, db_index=True)
    slug = models.SlugField(max_length=100, db_index=True)
    description = models.TextField()
    cost_ea = models.DecimalField(max_digits=6, decimal_places=2)
    cost_ws = models.DecimalField(max_digits=6, decimal_places=2)
    quantity = models.IntegerField()
    main_image = models.ImageField(upload_to = 'inventory/images/main/', null = True, blank = True)
    thumb_image = models.ImageField(upload_to = 'inventory/images/thumbs/', null = True, blank = True)
    product_category = models.ForeignKey('inventory.Product_Type')
    card_category = models.ForeignKey('inventory.Card_Type')
    last_modified = models.DateTimeField(auto_now = True, auto_now_add = True, null = True,  blank = True, editable = True)
    created = models.DateTimeField(auto_now_add = True, null = True, blank = True, editable = True, default = datetime.datetime.now())
    in_stock = models.BooleanField(default = False)

我想创建一个类似于此的表单字段(它将位于管理操作的中间页面上):

form.ChoiceField(选择= Inventory.get_all_verbose_names)

是否存在从模型中获取所有详细名称的函数?如果没有,我该怎么做呢?

1 个答案:

答案 0 :(得分:1)

您可以尝试类似

的内容
forms.ChoiceField(choices=((f.name, f.verbose_name) for f in Inventory._meta.fields))
相关问题