Skip to content

Fix PartRelated (based on updated AjaxCreateView class) - #1099

Merged
SchrodingersGat merged 5 commits into
inventree:masterfrom
eeintech:fix_related_parts
Nov 4, 2020
Merged

SchrodingersGat merged 5 commits into
inventree:masterfrom
eeintech:fix_related_parts

Conversation

@eeintech

@eeintech eeintech commented Nov 3, 2020

Copy link
Copy Markdown
Contributor

I think we should keep it, this method can be handy and PartRelated view relies on it.

@eeintech eeintech mentioned this pull request Nov 3, 2020
@SchrodingersGat

Copy link
Copy Markdown
Member

#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):

  • pre_save()
  • save()
  • post_save()

Now, this has been simplified to just save() - which can easily be overridden by a custom subclass.

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.object

I should have spotted that you had a post_save call in your PR, missed that one sorry.

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)

@SchrodingersGat

Copy link
Copy Markdown
Member

The refactoring is much simpler, I think. And importantly, it adds the ability to do custom form validation, by overriding the validate method and using the form.add_error call.

Previously, extra form validation had to be done in a custom post method which then required re-implementation of all the other stuff that happened in the top-level post call.

If you can adjust this PR to use the new save method I'll merge it in 👍

@SchrodingersGat

Copy link
Copy Markdown
Member

Actually, looking at this a bit closer, is there a need to even implement a custom save call here? The default save call just takes the form data and creates a database object, and the only thing you are doing is creating a new PartRelated object with the fields part_1 and part_2 (which the form validation would do anyway)...

I think perhaps that the custom save method could be deleted entirely here?

@eeintech

eeintech commented Nov 3, 2020

Copy link
Copy Markdown
Contributor Author

Oh I see, I missed the fact that the save method was now the way to go. I'll check if it is actually needed (like you mentioned) and refactor this PR as necessary.

@eeintech eeintech changed the title Restored post_save method in AjaxCreateView Fix PartRelated (based on updated AjaxCreateView class) Nov 4, 2020
@eeintech

eeintech commented Nov 4, 2020 •

Copy link
Copy Markdown
Contributor Author

@SchrodingersGat Alright, it's now fixed the right way 😄

@SchrodingersGat

Copy link
Copy Markdown
Member

Yeah, that looks a lot cleaner. Thanks for the fix 👍

@SchrodingersGat
SchrodingersGat merged commit 6c89a2a into inventree:master Nov 4, 2020
@eeintech
eeintech deleted the fix_related_parts branch July 14, 2021 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants