2012-12-10 43 views
0

列表的管理員ChoiceField形式的行動我有一個模型: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) 

我想創建表單域這樣的事情(這將是一個管理作用的中間頁):

form.ChoiceField(選擇= Inventory.get_all_verbose_names)

是否有存在從模型中獲取所有的詳細名稱的功能?如果沒有,我該怎麼做呢?

回答

1

你可以嘗試像

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

非常感謝您!完美工作。 – Jglstewart