Multi-page Django form with several models and views











up vote
1
down vote

favorite












I'm working on an existing system that has several forms that fill a group of models on Django, but they are related to one another and are split across some tab components to fill.



My objective is to make several fields required and a way to get that info anywhere on the system, if X form was already completely filled with the required fields or not. There is a validation on the forms, but it only happens if the user actually presses the save button, if he simply leaves the page or doesn't even enter that tab, the system ignores it.



Each form when completed is saving on the database that part individually, and that's good, because the user may not have all the info at once, but I need a way to alert:




  • If the user didn't fill all the forms to let him know that it will not be validated by an admin until he does so.

  • A flag so that the admin knows that the user didn't complete everything yet and doesn't waste time with it.


I tried to make a custom save function on the models, that would save a flag based if some values were not None, but since there are a lot of foreign relations and some models are filled on more than one page that could be hard.



Another thing I'm trying right now is getting the form instance on the main view, but for some reason or it's returning an incomplete form, or the is_valid() is not working on those instances.



I was also taking a look at FormWizard, but looks like it doesn't save on the database after each step, and is terribly complicated to implement on an already running system.



Any tips are welcome. Thanks.










share|improve this question






















  • If you're needing to do things in the for based on the values from another object, get the object in the view that renders your form & pass kwargs to your form that you can then use in the __init__ of the form to modify it. Shouldn't be a need to modify the save if thats what you want. Give it a go then post some code of specifically doesn't do what you want.
    – markwalker_
    2 days ago















up vote
1
down vote

favorite












I'm working on an existing system that has several forms that fill a group of models on Django, but they are related to one another and are split across some tab components to fill.



My objective is to make several fields required and a way to get that info anywhere on the system, if X form was already completely filled with the required fields or not. There is a validation on the forms, but it only happens if the user actually presses the save button, if he simply leaves the page or doesn't even enter that tab, the system ignores it.



Each form when completed is saving on the database that part individually, and that's good, because the user may not have all the info at once, but I need a way to alert:




  • If the user didn't fill all the forms to let him know that it will not be validated by an admin until he does so.

  • A flag so that the admin knows that the user didn't complete everything yet and doesn't waste time with it.


I tried to make a custom save function on the models, that would save a flag based if some values were not None, but since there are a lot of foreign relations and some models are filled on more than one page that could be hard.



Another thing I'm trying right now is getting the form instance on the main view, but for some reason or it's returning an incomplete form, or the is_valid() is not working on those instances.



I was also taking a look at FormWizard, but looks like it doesn't save on the database after each step, and is terribly complicated to implement on an already running system.



Any tips are welcome. Thanks.










share|improve this question






















  • If you're needing to do things in the for based on the values from another object, get the object in the view that renders your form & pass kwargs to your form that you can then use in the __init__ of the form to modify it. Shouldn't be a need to modify the save if thats what you want. Give it a go then post some code of specifically doesn't do what you want.
    – markwalker_
    2 days ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm working on an existing system that has several forms that fill a group of models on Django, but they are related to one another and are split across some tab components to fill.



My objective is to make several fields required and a way to get that info anywhere on the system, if X form was already completely filled with the required fields or not. There is a validation on the forms, but it only happens if the user actually presses the save button, if he simply leaves the page or doesn't even enter that tab, the system ignores it.



Each form when completed is saving on the database that part individually, and that's good, because the user may not have all the info at once, but I need a way to alert:




  • If the user didn't fill all the forms to let him know that it will not be validated by an admin until he does so.

  • A flag so that the admin knows that the user didn't complete everything yet and doesn't waste time with it.


I tried to make a custom save function on the models, that would save a flag based if some values were not None, but since there are a lot of foreign relations and some models are filled on more than one page that could be hard.



Another thing I'm trying right now is getting the form instance on the main view, but for some reason or it's returning an incomplete form, or the is_valid() is not working on those instances.



I was also taking a look at FormWizard, but looks like it doesn't save on the database after each step, and is terribly complicated to implement on an already running system.



Any tips are welcome. Thanks.










share|improve this question













I'm working on an existing system that has several forms that fill a group of models on Django, but they are related to one another and are split across some tab components to fill.



My objective is to make several fields required and a way to get that info anywhere on the system, if X form was already completely filled with the required fields or not. There is a validation on the forms, but it only happens if the user actually presses the save button, if he simply leaves the page or doesn't even enter that tab, the system ignores it.



Each form when completed is saving on the database that part individually, and that's good, because the user may not have all the info at once, but I need a way to alert:




  • If the user didn't fill all the forms to let him know that it will not be validated by an admin until he does so.

  • A flag so that the admin knows that the user didn't complete everything yet and doesn't waste time with it.


I tried to make a custom save function on the models, that would save a flag based if some values were not None, but since there are a lot of foreign relations and some models are filled on more than one page that could be hard.



Another thing I'm trying right now is getting the form instance on the main view, but for some reason or it's returning an incomplete form, or the is_valid() is not working on those instances.



I was also taking a look at FormWizard, but looks like it doesn't save on the database after each step, and is terribly complicated to implement on an already running system.



Any tips are welcome. Thanks.







django forms validation flags






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









Huskell

235




235












  • If you're needing to do things in the for based on the values from another object, get the object in the view that renders your form & pass kwargs to your form that you can then use in the __init__ of the form to modify it. Shouldn't be a need to modify the save if thats what you want. Give it a go then post some code of specifically doesn't do what you want.
    – markwalker_
    2 days ago


















  • If you're needing to do things in the for based on the values from another object, get the object in the view that renders your form & pass kwargs to your form that you can then use in the __init__ of the form to modify it. Shouldn't be a need to modify the save if thats what you want. Give it a go then post some code of specifically doesn't do what you want.
    – markwalker_
    2 days ago
















If you're needing to do things in the for based on the values from another object, get the object in the view that renders your form & pass kwargs to your form that you can then use in the __init__ of the form to modify it. Shouldn't be a need to modify the save if thats what you want. Give it a go then post some code of specifically doesn't do what you want.
– markwalker_
2 days ago




If you're needing to do things in the for based on the values from another object, get the object in the view that renders your form & pass kwargs to your form that you can then use in the __init__ of the form to modify it. Shouldn't be a need to modify the save if thats what you want. Give it a go then post some code of specifically doesn't do what you want.
– markwalker_
2 days ago












2 Answers
2






active

oldest

votes

















up vote
0
down vote













If I understand your problem well, then the solution I'd come up with is disabling the tabs of the Forms with HTML classes and javascript. So they should be under active and disabled tab classes. And maybe with 1 extra line of jQuery/javascript code, only with clicking on the submit buttons, the next tabs class should be changed to active from disabled class. So the user cannot leave the current active Tab until he does not click on the submit button, since all of the other tabs are disabled for him.



I think you can see and use a good simple example design from here:
https://bootsnipp.com/snippets/RlBxA



Few days ago I just wrote a long write-up on similar (but a very simple) Django Form which was designed with similar Tabs and should have been submitted via AJAX request to the Django view. Only one tab was active until the input fields were empty or not a valid input was used and submitted.
You can check that here too as an example: https://stackoverflow.com/a/53114080/7887576



I hope this can show you some easier and feasible direction on this task on a complex Form system.






share|improve this answer






























    up vote
    0
    down vote













    If I understand your requirements correctly what you want is a way of tracking down whether a user has filled out all of your forms or not. If that is the case I'd suggest adding a model whose purpose is to track down precisely that. Something in the form of:



    class UserFormTracking(models.Model):
    user = models.ForeignKey(settings.AUTH_MODEL, ..., unique=True)
    form1_completed = models.BooleanField(default=False)
    ...
    formn_completed = models.BooleanField(default=False)

    @property
    def all_completed(self):
    return self.form1_completed and ... and self.formk_completed


    Then I would override each form's save() method with something like the following (assuming this is the save() method of the K-th form):



    def save(self, user, **kwargs):
    instance = super(MyForm, self).save(**kwargs)
    forms_tracking_instance = UserFormTracking.objects.get_or_create(user=user)
    forms_tracking_instance.formk_completed = True
    forms_tracking_instance.save()

    return instance


    Note that you need to pass the user to the save() method. This would probably come out from the http request, but this could vary. Afterwards all that is remaining to check whether a user u has filled out every form is to query the UserFormTracking model for an instance whose user is u and every formk_completed is set to True.
    Bear in mind that in order to prevent inconsistencies you would probably want to execute the overriden save() methods of your forms inside a transaction.



    Of course this is a rather fixed solution. If your application is likely to change frequently (in the sense of adding or removing forms) you will need to come out with a more flexible solution. But you can follow the same idea.






    share|improve this answer























    • I like the idea, I'll try to implement something like this on my next work day and post the results here. Thanks.
      – Huskell
      2 days ago











    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%2f53238429%2fmulti-page-django-form-with-several-models-and-views%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    If I understand your problem well, then the solution I'd come up with is disabling the tabs of the Forms with HTML classes and javascript. So they should be under active and disabled tab classes. And maybe with 1 extra line of jQuery/javascript code, only with clicking on the submit buttons, the next tabs class should be changed to active from disabled class. So the user cannot leave the current active Tab until he does not click on the submit button, since all of the other tabs are disabled for him.



    I think you can see and use a good simple example design from here:
    https://bootsnipp.com/snippets/RlBxA



    Few days ago I just wrote a long write-up on similar (but a very simple) Django Form which was designed with similar Tabs and should have been submitted via AJAX request to the Django view. Only one tab was active until the input fields were empty or not a valid input was used and submitted.
    You can check that here too as an example: https://stackoverflow.com/a/53114080/7887576



    I hope this can show you some easier and feasible direction on this task on a complex Form system.






    share|improve this answer



























      up vote
      0
      down vote













      If I understand your problem well, then the solution I'd come up with is disabling the tabs of the Forms with HTML classes and javascript. So they should be under active and disabled tab classes. And maybe with 1 extra line of jQuery/javascript code, only with clicking on the submit buttons, the next tabs class should be changed to active from disabled class. So the user cannot leave the current active Tab until he does not click on the submit button, since all of the other tabs are disabled for him.



      I think you can see and use a good simple example design from here:
      https://bootsnipp.com/snippets/RlBxA



      Few days ago I just wrote a long write-up on similar (but a very simple) Django Form which was designed with similar Tabs and should have been submitted via AJAX request to the Django view. Only one tab was active until the input fields were empty or not a valid input was used and submitted.
      You can check that here too as an example: https://stackoverflow.com/a/53114080/7887576



      I hope this can show you some easier and feasible direction on this task on a complex Form system.






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        If I understand your problem well, then the solution I'd come up with is disabling the tabs of the Forms with HTML classes and javascript. So they should be under active and disabled tab classes. And maybe with 1 extra line of jQuery/javascript code, only with clicking on the submit buttons, the next tabs class should be changed to active from disabled class. So the user cannot leave the current active Tab until he does not click on the submit button, since all of the other tabs are disabled for him.



        I think you can see and use a good simple example design from here:
        https://bootsnipp.com/snippets/RlBxA



        Few days ago I just wrote a long write-up on similar (but a very simple) Django Form which was designed with similar Tabs and should have been submitted via AJAX request to the Django view. Only one tab was active until the input fields were empty or not a valid input was used and submitted.
        You can check that here too as an example: https://stackoverflow.com/a/53114080/7887576



        I hope this can show you some easier and feasible direction on this task on a complex Form system.






        share|improve this answer














        If I understand your problem well, then the solution I'd come up with is disabling the tabs of the Forms with HTML classes and javascript. So they should be under active and disabled tab classes. And maybe with 1 extra line of jQuery/javascript code, only with clicking on the submit buttons, the next tabs class should be changed to active from disabled class. So the user cannot leave the current active Tab until he does not click on the submit button, since all of the other tabs are disabled for him.



        I think you can see and use a good simple example design from here:
        https://bootsnipp.com/snippets/RlBxA



        Few days ago I just wrote a long write-up on similar (but a very simple) Django Form which was designed with similar Tabs and should have been submitted via AJAX request to the Django view. Only one tab was active until the input fields were empty or not a valid input was used and submitted.
        You can check that here too as an example: https://stackoverflow.com/a/53114080/7887576



        I hope this can show you some easier and feasible direction on this task on a complex Form system.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 2 days ago

























        answered 2 days ago









        Zollie

        28615




        28615
























            up vote
            0
            down vote













            If I understand your requirements correctly what you want is a way of tracking down whether a user has filled out all of your forms or not. If that is the case I'd suggest adding a model whose purpose is to track down precisely that. Something in the form of:



            class UserFormTracking(models.Model):
            user = models.ForeignKey(settings.AUTH_MODEL, ..., unique=True)
            form1_completed = models.BooleanField(default=False)
            ...
            formn_completed = models.BooleanField(default=False)

            @property
            def all_completed(self):
            return self.form1_completed and ... and self.formk_completed


            Then I would override each form's save() method with something like the following (assuming this is the save() method of the K-th form):



            def save(self, user, **kwargs):
            instance = super(MyForm, self).save(**kwargs)
            forms_tracking_instance = UserFormTracking.objects.get_or_create(user=user)
            forms_tracking_instance.formk_completed = True
            forms_tracking_instance.save()

            return instance


            Note that you need to pass the user to the save() method. This would probably come out from the http request, but this could vary. Afterwards all that is remaining to check whether a user u has filled out every form is to query the UserFormTracking model for an instance whose user is u and every formk_completed is set to True.
            Bear in mind that in order to prevent inconsistencies you would probably want to execute the overriden save() methods of your forms inside a transaction.



            Of course this is a rather fixed solution. If your application is likely to change frequently (in the sense of adding or removing forms) you will need to come out with a more flexible solution. But you can follow the same idea.






            share|improve this answer























            • I like the idea, I'll try to implement something like this on my next work day and post the results here. Thanks.
              – Huskell
              2 days ago















            up vote
            0
            down vote













            If I understand your requirements correctly what you want is a way of tracking down whether a user has filled out all of your forms or not. If that is the case I'd suggest adding a model whose purpose is to track down precisely that. Something in the form of:



            class UserFormTracking(models.Model):
            user = models.ForeignKey(settings.AUTH_MODEL, ..., unique=True)
            form1_completed = models.BooleanField(default=False)
            ...
            formn_completed = models.BooleanField(default=False)

            @property
            def all_completed(self):
            return self.form1_completed and ... and self.formk_completed


            Then I would override each form's save() method with something like the following (assuming this is the save() method of the K-th form):



            def save(self, user, **kwargs):
            instance = super(MyForm, self).save(**kwargs)
            forms_tracking_instance = UserFormTracking.objects.get_or_create(user=user)
            forms_tracking_instance.formk_completed = True
            forms_tracking_instance.save()

            return instance


            Note that you need to pass the user to the save() method. This would probably come out from the http request, but this could vary. Afterwards all that is remaining to check whether a user u has filled out every form is to query the UserFormTracking model for an instance whose user is u and every formk_completed is set to True.
            Bear in mind that in order to prevent inconsistencies you would probably want to execute the overriden save() methods of your forms inside a transaction.



            Of course this is a rather fixed solution. If your application is likely to change frequently (in the sense of adding or removing forms) you will need to come out with a more flexible solution. But you can follow the same idea.






            share|improve this answer























            • I like the idea, I'll try to implement something like this on my next work day and post the results here. Thanks.
              – Huskell
              2 days ago













            up vote
            0
            down vote










            up vote
            0
            down vote









            If I understand your requirements correctly what you want is a way of tracking down whether a user has filled out all of your forms or not. If that is the case I'd suggest adding a model whose purpose is to track down precisely that. Something in the form of:



            class UserFormTracking(models.Model):
            user = models.ForeignKey(settings.AUTH_MODEL, ..., unique=True)
            form1_completed = models.BooleanField(default=False)
            ...
            formn_completed = models.BooleanField(default=False)

            @property
            def all_completed(self):
            return self.form1_completed and ... and self.formk_completed


            Then I would override each form's save() method with something like the following (assuming this is the save() method of the K-th form):



            def save(self, user, **kwargs):
            instance = super(MyForm, self).save(**kwargs)
            forms_tracking_instance = UserFormTracking.objects.get_or_create(user=user)
            forms_tracking_instance.formk_completed = True
            forms_tracking_instance.save()

            return instance


            Note that you need to pass the user to the save() method. This would probably come out from the http request, but this could vary. Afterwards all that is remaining to check whether a user u has filled out every form is to query the UserFormTracking model for an instance whose user is u and every formk_completed is set to True.
            Bear in mind that in order to prevent inconsistencies you would probably want to execute the overriden save() methods of your forms inside a transaction.



            Of course this is a rather fixed solution. If your application is likely to change frequently (in the sense of adding or removing forms) you will need to come out with a more flexible solution. But you can follow the same idea.






            share|improve this answer














            If I understand your requirements correctly what you want is a way of tracking down whether a user has filled out all of your forms or not. If that is the case I'd suggest adding a model whose purpose is to track down precisely that. Something in the form of:



            class UserFormTracking(models.Model):
            user = models.ForeignKey(settings.AUTH_MODEL, ..., unique=True)
            form1_completed = models.BooleanField(default=False)
            ...
            formn_completed = models.BooleanField(default=False)

            @property
            def all_completed(self):
            return self.form1_completed and ... and self.formk_completed


            Then I would override each form's save() method with something like the following (assuming this is the save() method of the K-th form):



            def save(self, user, **kwargs):
            instance = super(MyForm, self).save(**kwargs)
            forms_tracking_instance = UserFormTracking.objects.get_or_create(user=user)
            forms_tracking_instance.formk_completed = True
            forms_tracking_instance.save()

            return instance


            Note that you need to pass the user to the save() method. This would probably come out from the http request, but this could vary. Afterwards all that is remaining to check whether a user u has filled out every form is to query the UserFormTracking model for an instance whose user is u and every formk_completed is set to True.
            Bear in mind that in order to prevent inconsistencies you would probably want to execute the overriden save() methods of your forms inside a transaction.



            Of course this is a rather fixed solution. If your application is likely to change frequently (in the sense of adding or removing forms) you will need to come out with a more flexible solution. But you can follow the same idea.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 2 days ago

























            answered 2 days ago









            ivissani

            264




            264












            • I like the idea, I'll try to implement something like this on my next work day and post the results here. Thanks.
              – Huskell
              2 days ago


















            • I like the idea, I'll try to implement something like this on my next work day and post the results here. Thanks.
              – Huskell
              2 days ago
















            I like the idea, I'll try to implement something like this on my next work day and post the results here. Thanks.
            – Huskell
            2 days ago




            I like the idea, I'll try to implement something like this on my next work day and post the results here. Thanks.
            – Huskell
            2 days ago


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238429%2fmulti-page-django-form-with-several-models-and-views%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            The Sandy Post

            Danny Elfman

            Pages that link to "Head v. Amoskeag Manufacturing Co."