Django - 带有Verbose_names列表的选择字段的管理员操作表单



我有一个模型:

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)

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

形式。选择字段(选择=Inventory.get_all_verbose_names)

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

你可以

尝试类似的东西

forms.ChoiceField(choices=((f.name, f.verbose_name) for f in Inventory._meta.fields))

最新更新