django formset初始数据显示id(主键)

时间:2011-10-11 16:08:22

标签: django

由于某些原因,我的表单中的排除不起作用,我的Item模型的主键显示在我的formset上。我怎么能摆脱它?

形式:

 41 class ItemForm(forms.ModelForm):
 53  
 54     class Meta:
 55         model = Item
 56         fields = ('name',
 58                   'description',
 59                   'quantity',
 60                   'start',
 61                   'end',
 62                   'cost_price',
 63                   'selling_price',)
 64         widgets = {'cost_price': forms.TextInput(attrs={'onChange':'updateSellingPrice()'}),
 65                    'description': forms.Textarea,
 66                    'start': SelectDateWidget,
 67                    'end': SelectDateWidget}
 68         exclude = ('id')
 69 
 70 ItemFormSet = modelformset_factory(Item, form=ItemForm, max_num=5, extra=3, exclude=('id'))

查看:

 59 def item_details(request, event_slug, role_id, module_slug):
 60     event = get_object_or_404(Event, slug=event_slug)
 61     payment_details = EventPaymentDetail.objects.get_or_create(event=event)[0]
 62     try:
 63         item_details = Item.objects.filter(event=event)
 65     except:
 66         item_details = Item.objects.get_or_create(event=event)[0]
 68 
 69     if request.method == 'POST':
 70         item_formset = ItemFormSet(request.POST)
 71 
 72         #display_error(request, item_formset)
 73         if item_formset.is_valid():
 74             instances = item_formset.save(commit=False)
 75             for instance in instances:
 76                 instance.event = event
 77                 instance.save()
 93             messages.success(request, 'Item details successfully saved!')
 94             url = reverse('event_admin_dashboard', args=[event_slug, role_id])
 95             return redirect(url)
 96 
 97     else:
 98         item_formset = ItemFormSet()
 99     currency_type = payment_details.currency
100     template = 'registration/item_details.html'
101     return render(request, template, locals())

1 个答案:

答案 0 :(得分:1)

我不相信可以排除id字段。我担心我不能给你解释或链接到文档。

除了:

这不是问题,但你错过了exclude元组的逗号。这不是问题,但这意味着django将其解释为

exclude = ('i', 'd')

应该是:

exclude = ('id',)