[Django]-Factory boy error : ValueError: save() prohibited to prevent data loss due to unsaved related object

9👍

It looks like all your factory classes inherit from factory.Factory. For example I see:

class UserFactoryCustomer(factory.Factory):

Since you are testing django models you should instead inherit from factory.django.DjangoModelFactory. So an example you posted would now look like:

class UserFactoryCustomer(factory.django.DjangoModelFactory):

    class Meta:
        model = User

    first_name = 'name'
    last_name = 'Asadov'
    username = factory.LazyAttribute(lambda o: slugify(o.first_name + '.' + o.last_name))
    email = factory.LazyAttribute(lambda a: '{0}.{1}@example.com'.format(a.first_name, a.last_name).lower())
    user_type = 1
    balance = 10000.00
👤aqs

0👍

I hate to break it to you, but I think this is a problem of missed indentation…

The super(User, self).save(*args, **kwargs) part of your save method for User isn’t actually in the Save method. That’s why it’s not actually being saved.

Leave a comment