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

Related object in creation action #8

Open
emigue opened this issue Aug 18, 2017 · 0 comments
Open

Related object in creation action #8

emigue opened this issue Aug 18, 2017 · 0 comments

Comments

@emigue
Copy link

emigue commented Aug 18, 2017

Related objects does not appear in previous_changes() method after object creation. For example, if your model has an "creator" field that references to auth.User and you set creator during creation, previous_changes() method does not show 'creator': [null, user].

To fix this problem, you could include next code in _save_state method:

    def _save_state(self, new_instance=False, event_type='save'):

        if new_instance and not self.pk:  # new instance
            self._states.append(self.new_state())
        else:
            # Pipe the pk on deletes so that a correct snapshot of the current
            # state can be taken.
            if event_type == DELETE:
                self.pk = None
            # Save current state.
            self._states.append(self.current_state())
        ...

And add the new function too:

    def new_state(self):
        """
        Returns a ``field -> None`` dict of the initial state of the instance.
        """
        field_names = set()
        [field_names.add(f.name) for f in self._meta.local_fields]
        [field_names.add(f.attname) for f in self._meta.local_fields]
        return dict([(field_name, None) for field_name in field_names])

If you are ok with that, I can make a PR to the library.

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

No branches or pull requests

1 participant