def test_options_caches_cleared_on_repoint(self): # Check that the relevant meta caches are cleared after a repoint # to ensure related field names are up to date. class NewModel2(models.Model): c = DeferredForeignKey() point(NewModel2, 'c', ModelA) repoint(NewModel2, 'c', ModelC) mdc_related = ModelC._meta.get_all_related_objects_with_model() found = False for rel, _ in mdc_related: if rel.model is NewModel2: found = True self.assertEqual(True, found)
def test_options_caches_not_cleared_on_repoint(self): # Check that the relevant meta caches are not cleared after a repoint # if clear_caches=False class NewModel2NoCache(models.Model): c = DeferredForeignKey() point(NewModel2NoCache, 'c', ModelA) repoint(NewModel2NoCache, 'c', ModelC, clean_caches=False) mdc_related = ModelC._meta.get_all_related_objects_with_model() found = False for rel, _ in mdc_related: if rel.model is NewModel2NoCache: found = True self.assertEqual(False, found)
class ModelCustomField(models.Model): fk = DeferredForeignKey(name='content2') other = CustomField(max_length=10) class NotAModel(object): pass class AbstractModel(models.Model): class Meta: abstract = True # Repoint ModelB's foreign key field 'fk' to ModelC repoint(ModelB, 'fk', ModelC) class ExistingTestCase(TestCase): def test_existing_fk(self): # We've repointed ModelB's 'fk' field to reference # ModelC, rather than ModelA. Check this has worked # as expected c = ModelC.objects.create() b = ModelB.objects.create(fk=c) b.save() self.assertEqual(c, b.fk) def test_not_a_model(self):