Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create save_instance method #89

Open
valdergallo opened this issue Nov 4, 2016 · 0 comments
Open

create save_instance method #89

valdergallo opened this issue Nov 4, 2016 · 0 comments

Comments

@valdergallo
Copy link
Owner

Update save to get another method that could be override

def save_instance(self, instance, **data):
        record = instance(**data)
        record.save(force_update=False)
        return record

    def save(self, instance=None):
        """
        Save all contents
        """
        if not instance:
            instance = self.Meta.model

        if not instance:
            raise AttributeError('Invalid instance model')

        if self.Meta.transaction:
            with transaction.atomic():
                for row, data in self.cleaned_data:
                    _ = self.save_instance(instance=instance, **data)

                try:
                    self.pre_commit()
                except Exception as e:
                    self._error.append(self.get_error_message(e, error_type='__pre_commit__'))
                    transaction.rollback()

                try:
                    transaction.commit()
                except Exception as e:
                    self._error.append(self.get_error_message(e, error_type='__trasaction__'))
                    transaction.rollback()

        else:
            for row, data in self.cleaned_data:
                _ = self.save_instance(instance=instance, **data)

        self.post_save_all_lines()

        return True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant