lundi 5 février 2018

Django Test: Model not updating

I am writing a test in django using django.test.TestCase. In setupTestData(cls), I create a User instance.

test_user1 = User(username='testuser',email="a@b.com")
test_user1.set_password('12345')
test_user1._category = 'Male'
test_user1.save()

I have also set up a class in myapp.models where I have a post_save signal to add an object to a model MyModel.

@receiver(post_save,sender=User)
def update_mymodel(sender,instance,created,**kwargs):
    MyModel.objects.create(user=instance,category=category)

The thing is, the receiver is called and the MyModel object should be created. But when I do MyModel.objects.all() in my test file, it shows empty. Why is the user instance not in the model?

Aucun commentaire:

Enregistrer un commentaire