0

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

art_cs
  • 683
  • 1
  • 8
  • 17
  • 1
    Does this answer your question? [Multiple Models in a single django ModelForm?](https://stackoverflow.com/questions/2770810/multiple-models-in-a-single-django-modelform) – Jim May 11 '20 at 13:15
  • before i asked , i searched , the link you mentioned work in updating two forms the instance exists , but what if the foreignKey instance still not exists ? – art_cs May 11 '20 at 13:23
  • look at the post again please – art_cs May 11 '20 at 22:01

1 Answers1

0

Doing it is simple, since you did not provide views.py and .html I had to give you my example.

views.py

def (request):
    if request.method == 'POST':
        # PROCESS POST METHOD FORM
        user_form = UserEditForm(instance=request.user, data=request.POST)
        profile_form = ProfileEditForm(instance=request.user.profile_user, data=request.POST, files=request.FILES)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
    else:
        # HERE WE ADD TWO FORMS IN CONTEXT
        user_form = UserEditForm(instance=request.user)
        profile_form = ProfileEditForm(instance=request.user.profile_user)

    return render(request, 'accounts/edit.html', {'user_form': user_form, 'profile_form': profile_form})

edit.html

<!-- Above Code -->

<form action="." method="post" enctype="multipart/form-data">
    {{ user_form.as_p }}
    {{ profile_form.as_p }}
    {% csrf_token %}
    <p><input type="submit" value="Save changes"></p>
</form>

<!-- Below Code -->

You can make two separate forms for two models.

Its better to use form prefix in such cases, in my case it will become:

def (request):
    if request.method == 'POST':
        # PROCESS POST METHOD FORM
        user_form = UserEditForm(request.POST, prefix="user_form")
        profile_form = ProfileEditForm(request.POST, prefix="profile_form", files=request.FILES)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
    else:
        # HERE WE ADD TWO FORMS IN CONTEXT
        user_form = UserEditForm(prefix="user_form")
        profile_form = ProfileEditForm(prefix="profile_form")

    return render(request, 'accounts/edit.html', {'user_form': user_form, 'profile_form': profile_form})
Hisham___Pak
  • 1,472
  • 1
  • 11
  • 23
  • but in my case the instance still not created (Computer) , i know using it for updating , but how to do the same thing if the (Computer) instance still not exists ?! – art_cs May 11 '20 at 13:21
  • read this https://stackoverflow.com/questions/18489393/django-submit-two-different-forms-with-one-submit-button – Hisham___Pak May 11 '20 at 13:26