Skip to content

Instantly share code, notes, and snippets.

@loic
Created April 18, 2014 20:20
Show Gist options
  • Save loic/11062658 to your computer and use it in GitHub Desktop.
Save loic/11062658 to your computer and use it in GitHub Desktop.
diff --git a/django/db/models/base.py b/django/db/models/base.py
index a0f43de..a6b5bb0 100644
--- a/django/db/models/base.py
+++ b/django/db/models/base.py
@@ -648,23 +648,25 @@ class Model(six.with_metaclass(ModelBase)):
"""
meta = cls._meta
for parent, field in meta.parents.items():
+ self._save_parents(cls=parent, using=using, update_fields=update_fields)
+
# Make sure the link fields are synced between parent and self.
- if (field and getattr(self, parent._meta.pk.attname) is None
- and getattr(self, field.attname) is not None):
+ if (getattr(self, field.attname) is not None):
setattr(self, parent._meta.pk.attname, getattr(self, field.attname))
- self._save_parents(cls=parent, using=using, update_fields=update_fields)
+
self._save_table(cls=parent, using=using, update_fields=update_fields)
+
# Set the parent's PK value to self.
- if field:
- setattr(self, field.attname, self._get_pk_val(parent._meta))
- # Since we didn't have an instance of the parent handy set
- # attname directly, bypassing the descriptor. Invalidate
- # the related object cache, in case it's been accidentally
- # populated. A fresh instance will be re-built from the
- # database if necessary.
- cache_name = field.get_cache_name()
- if hasattr(self, cache_name):
- delattr(self, cache_name)
+ setattr(self, field.attname, self._get_pk_val(parent._meta))
+
+ # Since we didn't have an instance of the parent handy set
+ # attname directly, bypassing the descriptor. Invalidate
+ # the related object cache, in case it's been accidentally
+ # populated. A fresh instance will be re-built from the
+ # database if necessary.
+ cache_name = field.get_cache_name()
+ if hasattr(self, cache_name):
+ delattr(self, cache_name)
def _save_table(self, raw=False, cls=None, force_insert=False,
force_update=False, using=None, update_fields=None):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment