Django

Code

Ticket #11448: clear_related_cache_v2.diff

File clear_related_cache_v2.diff, 3.2 kB (added by Dennis Kaarsemaker, 9 months ago)

updated patch, including testcase

  • django/db/models/fields/related.py

    old new  
    743743        cls._meta.duplicate_targets[self.column] = (target, "o2m") 
    744744 
    745745    def contribute_to_related_class(self, cls, related): 
     746        # Queries on related models will fill the _related_objects_cache, we 
     747        # need to clear it to make the new relation visible 
     748        if hasattr(cls._meta, '_related_objects_cache'): 
     749            del cls._meta._related_objects_cache 
    746750        setattr(cls, related.get_accessor_name(), ForeignRelatedObjectsDescriptor(related)) 
    747751 
    748752    def formfield(self, **kwargs): 
     
    782786        super(OneToOneField, self).__init__(to, to_field, OneToOneRel, **kwargs) 
    783787 
    784788    def contribute_to_related_class(self, cls, related): 
     789        # Queries on related models will fill the _related_objects_cache, we 
     790        # need to clear it to make the new relation visible 
     791        if hasattr(cls._meta, '_related_objects_cache'): 
     792            del cls._meta._related_objects_cache 
    785793        setattr(cls, related.get_accessor_name(), 
    786794                SingleRelatedObjectDescriptor(related)) 
    787795 
     
    946954        cls._meta.duplicate_targets[self.column] = (target, "m2m") 
    947955 
    948956    def contribute_to_related_class(self, cls, related): 
     957        # Queries on related models will fill the _related_m2m_cache, we 
     958        # need to clear it to make the new relation visible 
     959        if hasattr(cls._meta, '_related_many_to_many_cache'): 
     960            del cls._meta._related_many_to_many_cache 
    949961        # m2m relations to self do not have a ManyRelatedObjectsDescriptor, 
    950962        # as it would be redundant - unless the field is non-symmetrical. 
    951963        if related.model != related.parent_model or not self.rel.symmetrical: 
  • tests/regressiontests/query_between_definitions/__init__.py

    old new  
  • tests/regressiontests/query_between_definitions/models.py

    old new  
     1""" 
     2Bug 11448: Querying a model fills the _related_*_cache, making relationships  
     3added later invisible 
     4""" 
     5 
     6from django.db import models 
     7 
     8class City(models.Model): 
     9    name = models.CharField(max_length=50) 
     10    def __unicode__(self): 
     11        return self.name 
     12 
     13# We can't actually query as the table does not exist. 
     14# Next best thing is to manually call City._meta.init_name_map 
     15# City.objects.filter(name='Amsterdam') 
     16City._meta.init_name_map() 
     17 
     18class Street(models.Model): 
     19    city = models.ForeignKey(City) 
     20    name = models.CharField(max_length=50) 
     21    def __unicode__(self): 
     22        return self.name 
     23 
     24__test__  = {'API_TESTS':""" 
     25 
     26>>> City.objects.filter(street__name='Weteringschans') 
     27[] 
     28 
     29"""}