1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fix #19524 -- Incorrect caching of parents of unsaved model instances.

Thanks qcwxezdas for the report. Refs #13839.
This commit is contained in:
Aymeric Augustin
2012-12-28 23:16:13 +01:00
parent 37b3fd27ae
commit e9c24bef74
5 changed files with 48 additions and 4 deletions

View File

@@ -583,6 +583,15 @@ class Model(six.with_metaclass(ModelBase, object)):
if field:
setattr(self, field.attname, self._get_pk_val(parent._meta))
# Since we didn't have an instance of the parent handy, we
# 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)
if meta.proxy:
return

View File

@@ -692,7 +692,10 @@ class BaseInlineFormSet(BaseModelFormSet):
self.rel_name = RelatedObject(self.fk.rel.to, self.model, self.fk).get_accessor_name()
if queryset is None:
queryset = self.model._default_manager
qs = queryset.filter(**{self.fk.name: self.instance})
if self.instance.pk:
qs = queryset.filter(**{self.fk.name: self.instance})
else:
qs = queryset.none()
super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix,
queryset=qs, **kwargs)