[英]django creating models inside of form for manytomany field
我正在制作食譜網站,因此我要為其創建食譜模型,如下所示:
class Recipe(models.Model):
....
products = models.ManyToManyField(ProductMeasurement)
....
我已經有了ProductMeasurement模型和表格:
class ProductMeasurement(models.Model):
product = models.ForeignKey(Product)
measurement = models.ForeignKey(Measurement)
value = models.FloatField()
class FridgeForm(forms.Form):
product = forms.ModelChoiceField(queryset=None, widget=forms.Select(attrs={'class': 'form-control'}))
amount = forms.DecimalField(decimal_places=2, widget=forms.NumberInput(attrs={'class': 'form-control'}))
measurement = forms.ModelChoiceField(queryset=None, widget=forms.Select(attrs={'class': 'form-control'}))
def __init__(self, *args, **kwargs):
super(FridgeForm, self).__init__(*args, **kwargs)
self.fields['product'].queryset = Product.objects.all()
self.fields['measurement'].queryset = Measurement.objects.all()
我想要做的是為配方模型創建表單。 問題在於用戶將不再需要其食譜所需的ProductMeasurements。 因此,我需要找到一種方法,使用戶可以在Recipe表單內創建ProductMeasurement。 這里有什么可能的方法?
django formets是我想要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.