Repository navigation
Fix PartRelated (based on updated AjaxCreateView class) - #1099
Conversation
|
#1063 refactored the way that forms are handled in a major way. One part of this was "simplifying" the form saving hooks. Prior to this, the form would call (after validation):
Now, this has been simplified to just The default implementation of save() is pretty simple: def save(self, form):
"""
Method for actually saving the form to the database.
Default implementation is very simple,
but can be overridden if required.
"""
self.object = form.save()
return self.objectI should have spotted that you had a Here's what it is currently: def post_save(self):
""" Save PartRelated model (POST method does not) """
form = self.get_form()
if form.is_valid():
part_1 = form.cleaned_data['part_1']
part_2 = form.cleaned_data['part_2']
PartRelated.create(part_1, part_2)With the new system, it should be: def save(self, form, **kwargs):
""" Save PartRelated model """
# Note: form.is_valid() is already true by the time save() is called!
# if form.is_valid():
# Also, you could run some custom form validation before getting to save(),
# by overriding the new 'validate' method
part_1 = form.cleaned_data['part_1']
part_2 = form.cleaned_data['part_2']
PartRelated.create(part_1, part_2) |
|
The refactoring is much simpler, I think. And importantly, it adds the ability to do custom form validation, by overriding the Previously, extra form validation had to be done in a custom If you can adjust this PR to use the new |
|
Actually, looking at this a bit closer, is there a need to even implement a custom save call here? The default I think perhaps that the custom |
|
Oh I see, I missed the fact that the |
…d InvenTree CreateAjaxView, improved part related testing
|
@SchrodingersGat Alright, it's now fixed the right way 😄 |
|
Yeah, that looks a lot cleaner. Thanks for the fix 👍 |
I think we should keep it, this method can be handy and PartRelated view relies on it.