Django inline formset multiple models











up vote
0
down vote

favorite












TL;DR: I need a some kind of formset for formsets.



I have two different models related to one buisness-entity, and I need to make a form to edit both models like a one form. And I need to create a lot of such forms on the one page like djngo inline formset does.



Now I have the following thing:



class Parent(models.Model):
name = models.Charfield()

class FirstChild(models.Model):
name = models.Charfield()
e_id = models.IntegerField()
parent = models.ForeignKey(Parent)

class FirstChildForm(django.forms.ModelForm):
class Meta:
model = Child
fields = ('name', 'e_id', 'parent')
widgets = {'parent': forms.TextInput}


And I render a lot of them usiing inline formsets:



formset_class = inlineformset_factory(Parent, FirstChild, 
form=FirstChildForm, extra=1)


But now I have to add second child model and a form for it, and still render it like an one inline form, bit make it form actually edit two models. Like this:



class SecondChild(models.Model):
name = models.Charfield()
e_id = models.IntegerField()
parent = models.ForeignKey(Parent)

class SecondChildForm(django.forms.ModelForm):
class Meta:
model = Child
fields = ('name', 'e_id', 'parent')
widgets = {'parent': forms.TextInput}

formset_class = inlineformset_factory(models=[Parent, FirstChild],
forms=[FirstChildForm, SecondChildForm],
extra=1)


As far as I understand, Django formsets cannot work with multiple models right now.



So which way should I choose to implement this behaviour and do not broke all django conceptions? I cannot use some extra libraries so I have to implement everything by myself and I use django 1.6 if it is important.










share|improve this question


























    up vote
    0
    down vote

    favorite












    TL;DR: I need a some kind of formset for formsets.



    I have two different models related to one buisness-entity, and I need to make a form to edit both models like a one form. And I need to create a lot of such forms on the one page like djngo inline formset does.



    Now I have the following thing:



    class Parent(models.Model):
    name = models.Charfield()

    class FirstChild(models.Model):
    name = models.Charfield()
    e_id = models.IntegerField()
    parent = models.ForeignKey(Parent)

    class FirstChildForm(django.forms.ModelForm):
    class Meta:
    model = Child
    fields = ('name', 'e_id', 'parent')
    widgets = {'parent': forms.TextInput}


    And I render a lot of them usiing inline formsets:



    formset_class = inlineformset_factory(Parent, FirstChild, 
    form=FirstChildForm, extra=1)


    But now I have to add second child model and a form for it, and still render it like an one inline form, bit make it form actually edit two models. Like this:



    class SecondChild(models.Model):
    name = models.Charfield()
    e_id = models.IntegerField()
    parent = models.ForeignKey(Parent)

    class SecondChildForm(django.forms.ModelForm):
    class Meta:
    model = Child
    fields = ('name', 'e_id', 'parent')
    widgets = {'parent': forms.TextInput}

    formset_class = inlineformset_factory(models=[Parent, FirstChild],
    forms=[FirstChildForm, SecondChildForm],
    extra=1)


    As far as I understand, Django formsets cannot work with multiple models right now.



    So which way should I choose to implement this behaviour and do not broke all django conceptions? I cannot use some extra libraries so I have to implement everything by myself and I use django 1.6 if it is important.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      TL;DR: I need a some kind of formset for formsets.



      I have two different models related to one buisness-entity, and I need to make a form to edit both models like a one form. And I need to create a lot of such forms on the one page like djngo inline formset does.



      Now I have the following thing:



      class Parent(models.Model):
      name = models.Charfield()

      class FirstChild(models.Model):
      name = models.Charfield()
      e_id = models.IntegerField()
      parent = models.ForeignKey(Parent)

      class FirstChildForm(django.forms.ModelForm):
      class Meta:
      model = Child
      fields = ('name', 'e_id', 'parent')
      widgets = {'parent': forms.TextInput}


      And I render a lot of them usiing inline formsets:



      formset_class = inlineformset_factory(Parent, FirstChild, 
      form=FirstChildForm, extra=1)


      But now I have to add second child model and a form for it, and still render it like an one inline form, bit make it form actually edit two models. Like this:



      class SecondChild(models.Model):
      name = models.Charfield()
      e_id = models.IntegerField()
      parent = models.ForeignKey(Parent)

      class SecondChildForm(django.forms.ModelForm):
      class Meta:
      model = Child
      fields = ('name', 'e_id', 'parent')
      widgets = {'parent': forms.TextInput}

      formset_class = inlineformset_factory(models=[Parent, FirstChild],
      forms=[FirstChildForm, SecondChildForm],
      extra=1)


      As far as I understand, Django formsets cannot work with multiple models right now.



      So which way should I choose to implement this behaviour and do not broke all django conceptions? I cannot use some extra libraries so I have to implement everything by myself and I use django 1.6 if it is important.










      share|improve this question













      TL;DR: I need a some kind of formset for formsets.



      I have two different models related to one buisness-entity, and I need to make a form to edit both models like a one form. And I need to create a lot of such forms on the one page like djngo inline formset does.



      Now I have the following thing:



      class Parent(models.Model):
      name = models.Charfield()

      class FirstChild(models.Model):
      name = models.Charfield()
      e_id = models.IntegerField()
      parent = models.ForeignKey(Parent)

      class FirstChildForm(django.forms.ModelForm):
      class Meta:
      model = Child
      fields = ('name', 'e_id', 'parent')
      widgets = {'parent': forms.TextInput}


      And I render a lot of them usiing inline formsets:



      formset_class = inlineformset_factory(Parent, FirstChild, 
      form=FirstChildForm, extra=1)


      But now I have to add second child model and a form for it, and still render it like an one inline form, bit make it form actually edit two models. Like this:



      class SecondChild(models.Model):
      name = models.Charfield()
      e_id = models.IntegerField()
      parent = models.ForeignKey(Parent)

      class SecondChildForm(django.forms.ModelForm):
      class Meta:
      model = Child
      fields = ('name', 'e_id', 'parent')
      widgets = {'parent': forms.TextInput}

      formset_class = inlineformset_factory(models=[Parent, FirstChild],
      forms=[FirstChildForm, SecondChildForm],
      extra=1)


      As far as I understand, Django formsets cannot work with multiple models right now.



      So which way should I choose to implement this behaviour and do not broke all django conceptions? I cannot use some extra libraries so I have to implement everything by myself and I use django 1.6 if it is important.







      django forms






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 11:08









      Paul

      2,01842235




      2,01842235
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          So, finally I used this approach as a base: https://micropyramid.com/blog/how-to-use-nested-formsets-in-django/






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410815%2fdjango-inline-formset-multiple-models%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            So, finally I used this approach as a base: https://micropyramid.com/blog/how-to-use-nested-formsets-in-django/






            share|improve this answer

























              up vote
              0
              down vote



              accepted










              So, finally I used this approach as a base: https://micropyramid.com/blog/how-to-use-nested-formsets-in-django/






              share|improve this answer























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                So, finally I used this approach as a base: https://micropyramid.com/blog/how-to-use-nested-formsets-in-django/






                share|improve this answer












                So, finally I used this approach as a base: https://micropyramid.com/blog/how-to-use-nested-formsets-in-django/







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 28 at 11:49









                Paul

                2,01842235




                2,01842235






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410815%2fdjango-inline-formset-multiple-models%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Berounka

                    Sphinx de Gizeh

                    Different font size/position of beamer's navigation symbols template's content depending on regular/plain...