简体   繁体   English

使用modelformset_factory将数据添加到Django表单类

[英]Add data to Django form class using modelformset_factory

I have a problem where I need to display a lot of forms for detail data for a hierarchical data set. 我有一个问题,需要为分层数据集显示很多详细数据表格。 I want to display some relational fields as labels for the forms and I'm struggling with a way to do this in a more robust way. 我想显示一些关系字段作为表单的标签,而我正在努力以一种更可靠的方式来做到这一点。 Here is the code... 这是代码...

class Category(models.Model):
  name = models.CharField(max_length=160)

class Item(models.Model):
  category = models.ForeignKey('Category')
  name = models.CharField(max_length=160)
  weight = models.IntegerField(default=0)

  class Meta:
    ordering = ('category','weight','name')

class BudgetValue(models.Model):
  value = models.IntegerField()
  plan = models.ForeignKey('Plan')
  item = models.ForeignKey('Item')

I use the modelformset_factory to create a formset of budgetvalue forms for a particular plan. 我使用modelformset_factory为特定计划创建预算值表单的表单集。 What I'd like is item name and category name for each BudgetValue. 我想要的是每个BudgetValue的商品名称和类别名称。 When I iterate through the forms each one will be labeled properly. 当我遍历表格时,每个表格都将正确标记。

class BudgetValueForm(forms.ModelForm):
item = forms.ModelChoiceField(queryset=Item.objects.all(),widget=forms.HiddenInput())
plan = forms.ModelChoiceField(queryset=Plan.objects.all(),widget=forms.HiddenInput())

category = "" < assign dynamically on form creation >
item = "" < assign dynamically on form creation >
class Meta:
    model = BudgetValue
    fields = ('item','plan','value')

What I started out with is just creating a dictionary of budgetvalue.item.category.name, budgetvalue.item.name, and the form for each budget value. 我开始只是创建一个budgetvalue.item.category.name,budgetvalue.item.name以及每个预算值的形式的字典。 This gets passed to the template and I render it as I intended. 这将传递到模板,然后按预期进行渲染。 I'm assuming that the ordering of the forms in the formset and the querset used to genererate the formset keep the budgetvalues in the same order and the dictionary is created correctly. 我假设表单集中的表单顺序和用于生成表单集的查询集使预算值保持相同的顺序,并且正确创建了字典。 That is the budgetvalue.item.name is associated with the correct form. 也就是说budgetvalue.item.name与正确的格式相关联。 This scares me and I'm thinking there has to be a better way. 这让我感到恐惧,我想必须有一个更好的方法。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Well, I figured out the answer to my own question. 好吧,我想出了我自己问题的答案。 I've overridden the init class on the form and accessed the instance of the model form. 我已经覆盖了表单上的init类,并访问了模型表单的实例。 Works exactly as I wanted and it was easy. 完全按照我的要求工作,这很容易。

class BudgetValueForm(forms.ModelForm):
  item = forms.ModelChoiceField(queryset=Item.objects.all(),widget=forms.HiddenInput())
  plan = forms.ModelChoiceField(queryset=Plan.objects.all(),widget=forms.HiddenInput())

  def __init__(self, *args, **kwargs): 
    super(BudgetValueForm, self).__init__(*args, **kwargs) 
    self.itemname = self.instance.item.name 
    self.categoryname = self.instance.item.category.name 

  class Meta:
    model = BudgetValue
    fields = ('item','plan','value')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM