0

I am testing a registration function in my Django app. When users register, a default user_type is designated to the user. I am using a post_save signal to process this.

I have a Profile model to be connected with the User and under the Profile model, is a field user_type which is from another model.

class UserType(models.Model):
    name = models.CharField(max_length=50, blank=True)

    def __str__(self):
        return self.name

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    user_type = models.ForeignKey(UserType, on_delete=models.SET_NULL, null=True, blank=True)

To automatically assign a default user_type, I have a signal:

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        user_type = UserType.objects.get(pk=1)
        Profile.objects.create(
            user=instance,
            user_type=user_type)

I made a fixture of my User, Profile, and UserType models and in my APITestCase, I placed the fixture.

class TestUserManagement(APITestCase):
    fixtures = ['fixtures/initial']

When I run the test, the fixtures does not seem to load to the test database.

  File "/usr/src/app/fixtures/signals.py", line 15, in create_user_profile
    user_type = UserType.objects.get(pk=1)

...

{model}.UserType.DoesNotExist: Problem installing fixture 
'/usr/src/app/fixtures/initial.json': UserType matching query does not exist.
Nikko
  • 1,410
  • 1
  • 22
  • 49

0 Answers0