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.
django forms validation flags
add a comment |
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.
django forms validation flags
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 & passkwargsto 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
add a comment |
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.
django forms validation flags
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
django forms validation flags
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 & passkwargsto 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
add a comment |
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 & passkwargsto 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
add a comment |
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.
add a comment |
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.
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited 2 days ago
answered 2 days ago
Zollie
28615
28615
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
kwargsto 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