question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A simple MyFactory.create() actually not saving in DB? (Django)

See original GitHub issue

Hello,

I just gave a try to factory_boy, and it fails to actually save my model in DB.

Here’s my model:

# myapp.models.py
from custom_user.models import AbstractEmailUser


class User(AbstractEmailUser):
    class Meta:
        verbose_name = _(u'Utilisateur')

Here’s my factory:

# myapp.factories.py
import factory

from myapp.models import User


class UserFactory(factory.Factory):
    class Meta:
        model = User

    email = factory.Sequence(
        lambda n: 'user_%d@mail.com' % n)

What I’m reading from the docs:

# Returns a saved User instance
user = UserFactory.create()

And here’s how I’m trying to use the whole thing:

  • Vanilla style, everything works just fine:
In : User.objects.all().count()
Out: 15
In : User.objects.create(email="test@mail.com")
In : User.objects.all().count()
Out: 16
  • With factory_boy:
In : User.objects.all().count()
Out: 16
In : user_created_with_factory = UserFactory()
In : User.objects.all().count()
Out: 16
In : user_created_with_factory.id
In : user_created_with_factory.email
Out: 'user_24@mail.com'

That’s where it should return 17 for user_created_with_factory.id I also tried with UserFactory.create() instead of UserFactory(), same result. Of course, if I try user_created_with_factory.save(), I’ll have my instance in DB and have an ID…

My env: python: 2.7.6 Django: 1.10 Pip: pip 1.5.4 Virtualenv: 1.11.4 factory-boy: 2.7.0

Any ideas of what could be wrong with my procedure !? Thanks

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

32reactions
rbarroiscommented, Nov 26, 2016

Hi!

If you work with Django models, you need to use factory.django.DjangoModelFactory — otherwise, factory_boy uses the “simple Python object” approach.

Note that factory_boy also supports other ORMs (SQLAlchemy, …).

Hope this helps!

1reaction
rbarroiscommented, Jul 9, 2019

@jyr since this issue has already been closed, it would be easier for us to track it if you open a separate issue.

Also, your use of location = LocationFactory() won’t work the way you expect it to; you should be using location = factory.SubFactory(LocationFactory).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to let DjangoModelFactory create a model without saving ...
Is there a way that I can make the factory create an object without saving it to the DB yet? I looked over...
Read more >
Using factory_boy with ORMs - Read the Docs
All factories for a Django Model should use the DjangoModelFactory base class. ... the create() function won't “save” it, since this wouldn't make...
Read more >
Test factory functions in Django - lukeplant.me.uk
Using Django ORM create calls directly in your tests is not a great solution, because database constraints often force you to specify fields ......
Read more >
Django Factory Audit - James Cooke
Calls full_clean on the Item instance created by Factory Boy which it wraps. This raises ValidationError and the Item is not saved. UserFactory ......
Read more >
Factory Boy Documentation - Read the Docs
X versions, the create will actually call AssociatedClass.objects.create, as for a Django model. Starting from 2.0, factory.Factory.create() ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found