Ticket #9023: onetoone_cache_clear.diff

File onetoone_cache_clear.diff, 2.2 KB (added by juriejan, 6 years ago)

First sloppy version of the patch (needs feedback)

  • fields/related.py

     
    114114        self.set_attributes_from_rel()
    115115        related = RelatedObject(other, cls, self)
    116116        if not cls._meta.abstract:
    117             self.contribute_to_related_class(other, related)
     117            self.related_object_descriptor = self.contribute_to_related_class(other, related)
    118118
    119119    def get_db_prep_lookup(self, lookup_type, value):
    120120        # If we are doing a lookup on a Related Field, we must be
     
    271271                                (value, instance._meta.object_name,
    272272                                 self.field.name, self.field.rel.to._meta.object_name))
    273273
     274        # Remove the cache for the related field in the related object
     275        related_object = getattr(instance, self.field.name)
     276        if related_object:
     277            cache_name = self.field.related_object_descriptor.cache_name
     278            if hasattr(related_object, cache_name):
     279                delattr(related_object, cache_name)
     280
    274281        # Set the value of the related field
    275282        try:
    276283            val = getattr(value, self.field.rel.get_related_field().attname)
     
    719726        cls._meta.duplicate_targets[self.column] = (target, "o2m")
    720727
    721728    def contribute_to_related_class(self, cls, related):
    722         setattr(cls, related.get_accessor_name(), ForeignRelatedObjectsDescriptor(related))
     729        descriptor = ForeignRelatedObjectsDescriptor(related)
     730        setattr(cls, related.get_accessor_name(), descriptor)
     731        return descriptor
    723732
    724733    def formfield(self, **kwargs):
    725734        defaults = {
     
    758767        super(OneToOneField, self).__init__(to, to_field, OneToOneRel, **kwargs)
    759768
    760769    def contribute_to_related_class(self, cls, related):
    761         setattr(cls, related.get_accessor_name(),
    762                 SingleRelatedObjectDescriptor(related))
     770        descriptor = SingleRelatedObjectDescriptor(related)
     771        setattr(cls, related.get_accessor_name(), descriptor)
     772        return descriptor
    763773
    764774    def formfield(self, **kwargs):
    765775        if self.rel.parent_link:
Back to Top