Updated i have two tables(models) one for product name and features and the other for prices i normalized to database tables
like this
class Model(models.Model):
name = models.CharField(max_length=23)
def __str__(self):
return self.name
class Feature(models.Model):
model = models.ForeignKey(Model,on_delete=models.CASCADE)
price = models.IntegerField()
company = models.CharField(max_length=33)
def __str__(self):
return self.model
my views.py
def create(request):
createmodel = ModelForms(prefix='createmodel')
createfeature = FeatureForm(prefix='createfeature')
if request.method == 'POST':
createmodel = ModelForms(request.POST,prefix='createmodel')
if createmodel.is_valid():
createmodel.save()
name = createmodel.cleaned_data['name']
if createfeature.is_valid():
createfeature = FeatureForm(request.POST,prefix='createfeature',model=name)
createfeature.save()
return redirect('/lists/')
return render(request,'forms/create.html',{'createmodel':createmodel,'createfeature':createfeature})
my forms.py
class ModelForms(forms.ModelForm):
class Meta:
model = Model
fields = [
'name'
]
class FeatureForm(forms.ModelForm):
read_only = ['model']
class Meta:
model = Feature
fields = [
'price','company'
]
but the form wont saved
how to submit both forms at the same time with providing the exact foreign key (as filled in ModelForm
to FeatureForm
)in one go
i know how to do it in updating two forms , but how to create submit two forms with providing the instance directly
much respect