In django I've made this inlineformset_factory and I tried adding a widget to one of the fields. But I get the error that it is missing "attrs". I then add the "attrs" and I get another error saying "unexpected keyword argument 'attrs'"
Here is the inlineformset_factory.
UNIT_CHOICES = (('dl', 'dl'), ('l', 'l'), ('kg', 'kg'),)
IngredientFormSet = inlineformset_factory(RecipeModel, Ingredient, can_delete=False, extra=MAX_INGREDIENTS, exclude=[],
widgets={'unit_type': forms.ChoiceField(attrs={}, label="Måleenhet", required=True, widget=forms.Select, choices=UNIT_CHOICES)})
Here is the stuff I left out.
forms.py:
class AddRecipeForm(ModelForm):
products = Product.objects.all()
recipes = RecipeModel.objects.all()
choices = ()
iterate = 1
for product in products:
if not recipes.filter(title=product.name).exists():
choices = ((product.name, product.name),) + choices
iterate += 1
title = forms.ChoiceField(label="Navn på produktet til oppskriften", required=True, widget=forms.Select, choices=choices)
class Meta:
model = RecipeModel
exclude = []
class IngredientForm(ModelForm):
title = forms.CharField(max_length=255, widget=TextInput(attrs={'id': 'ingredient_title',
'class': 'ingredient_class'}))
class Meta:
model = Ingredient
exclude = ['recipe']
views.py
def add_recipe(request):
if request.POST:
form = AddRecipeForm(request.POST)
if form.is_valid():
recipe = form.save(commit=False)
ingredient_formset = IngredientFormSet(request.POST, instance=recipe)
if ingredient_formset.is_valid():
recipe.save()
ingredient_formset.save()
return redirect('admin_site')
else:
return redirect('admin_site')
else:
return redirect('admin_site')
else:
form = AddRecipeForm()
ingredient_formset = IngredientFormSet(instance=RecipeModel())
return render(request, 'admin/add_recipe.html', {
'form': form,
'ingredient_formset': ingredient_formset,
}, context_instance=RequestContext(request))
models.py
class RecipeModel(models.Model):
title = models.CharField("Navn på produktet", max_length=255, default="")
kilogram_bottle = models.FloatField("Kg råvare for et produkt", blank=True)
deciliter_bottle = models.FloatField("Deciliter råvare for et produkt", blank=True)
description = models.TextField("Oppskrifts detaljer", default="")
class Ingredient(models.Model):
recipe = models.ForeignKey(RecipeModel, on_delete=models.CASCADE)
ingredient_title = models.CharField("Navn på ingrediens", max_length=255, default="")
unit_before = models.FloatField("Råvare", blank=True, null=True)
unit_after = models.FloatField("Ferdig behandlet råvare", blank=True, null=True)
unit_type = models.CharField("Måleenhet", max_length=255)
Since you are specifying the widgets, you do not have to specify the field type again.
widgets={
'unit_type': forms.Select
}
But it looks like you are trying to do a little more with the field, so you can create a modelform, and pass that as an argument.
class RecipeForm(forms.ModelForm):
...
forms.ChoiceField(label="Måleenhet", required=True, widget=forms.Select(attrs={}), choices=UNIT_CHOICES)
inlineformset_factory(RecipeModel, Ingredient, form=RecipeForm, can_delete=False, extra=MAX_INGREDIENTS)
You can read more on this topic here
Also, note that attrs
is an attribute on the widget, not the form field
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.