def test_fixture_loading(self):
     with app_cache_restorer():
         call_command(
             'loaddata', 'test_fk_to_loading.json',
             verbosity=0, commit=False
         )
     to_model_def = ModelDefinition.objects.get_by_natural_key(
         'tests', 'tomodel'
     )
     to_model_class = to_model_def.model_class()
     # Make sure the origin's model class was created
     self.assertTrue(hasattr(to_model_class, 'froms'))
     try:
         from_model_class = to_model_class.froms.field.model
     except AttributeError:
         from_model_class = get_related_model(to_model_class.froms.related)
     try:
         fk_field = from_model_class._meta.get_field('fk')
     except FieldDoesNotExist:
         self.fail('The fk field should be created')
     to_model_class = to_model_def.model_class()
     self.assertEqual(get_remote_field_model(fk_field), to_model_class)
     to_instance = to_model_class.objects.create()
     from_instance = from_model_class.objects.create(fk=to_instance)
     self.assertEqual(to_instance.froms.get(), from_instance)
     to_instance.delete()
     with self.assertRaises(from_model_class.DoesNotExist):
         from_model_class.objects.get(pk=from_instance.pk)
Beispiel #2
0
 def test_fixture_loading(self):
     with app_cache_restorer():
         call_command(
             'loaddata',
             'test_fk_to_loading.json',
             verbosity=0,
         )
     to_model_def = ModelDefinition.objects.get_by_natural_key(
         'tests', 'tomodel')
     to_model_class = to_model_def.model_class()
     # Make sure the origin's model class was created
     self.assertTrue(hasattr(to_model_class, 'froms'))
     try:
         from_model_class = to_model_class.froms.field.model
     except AttributeError:
         from_model_class = to_model_class.froms.related.related_model
     try:
         fk_field = from_model_class._meta.get_field('fk')
     except FieldDoesNotExist:
         self.fail('The fk field should be created')
     to_model_class = to_model_def.model_class()
     self.assertEqual(get_remote_field_model(fk_field), to_model_class)
     to_instance = to_model_class.objects.create()
     from_instance = from_model_class.objects.create(fk=to_instance)
     self.assertEqual(to_instance.froms.get(), from_instance)
     to_instance.delete()
     with self.assertRaises(from_model_class.DoesNotExist):
         from_model_class.objects.get(pk=from_instance.pk)
 def test_recursive_relationship(self):
     fk = ForeignKeyDefinition.objects.create(
         model_def=self.model_def, name='f1', null=True, blank=True,
         to=self.model_def
     )
     self.assertTrue(fk.is_recursive_relationship)
     Model = self.model_def.model_class()
     self.assertEqual(get_remote_field_model(Model._meta.get_field('f1')), Model)
     obj1 = Model.objects.create()
     obj2 = Model.objects.create(f1=obj1)
     obj1.f1 = obj2
     obj1.save()
Beispiel #4
0
 def test_recursive_relationship(self):
     fk = ForeignKeyDefinition.objects.create(model_def=self.model_def,
                                              name='f1',
                                              null=True,
                                              blank=True,
                                              to=self.model_def)
     self.assertTrue(fk.is_recursive_relationship)
     Model = self.model_def.model_class()
     self.assertEqual(get_remote_field_model(Model._meta.get_field('f1')),
                      Model)
     obj1 = Model.objects.create()
     obj2 = Model.objects.create(f1=obj1)
     obj1.f1 = obj2
     obj1.save()