Ticket #11448: clear_related_cache_v2.diff
File clear_related_cache_v2.diff, 3.2 KB (added by , 15 years ago) |
---|
-
django/db/models/fields/related.py
743 743 cls._meta.duplicate_targets[self.column] = (target, "o2m") 744 744 745 745 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 746 750 setattr(cls, related.get_accessor_name(), ForeignRelatedObjectsDescriptor(related)) 747 751 748 752 def formfield(self, **kwargs): … … 782 786 super(OneToOneField, self).__init__(to, to_field, OneToOneRel, **kwargs) 783 787 784 788 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 785 793 setattr(cls, related.get_accessor_name(), 786 794 SingleRelatedObjectDescriptor(related)) 787 795 … … 946 954 cls._meta.duplicate_targets[self.column] = (target, "m2m") 947 955 948 956 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 949 961 # m2m relations to self do not have a ManyRelatedObjectsDescriptor, 950 962 # as it would be redundant - unless the field is non-symmetrical. 951 963 if related.model != related.parent_model or not self.rel.symmetrical: -
tests/regressiontests/query_between_definitions/__init__.py
1 -
tests/regressiontests/query_between_definitions/models.py
1 """ 2 Bug 11448: Querying a model fills the _related_*_cache, making relationships 3 added later invisible 4 """ 5 6 from django.db import models 7 8 class 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') 16 City._meta.init_name_map() 17 18 class 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 """}