A simple MyFactory.create() actually not saving in DB? (Django)
See original GitHub issueHello,
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:
- Created 7 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!
@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 usinglocation = factory.SubFactory(LocationFactory)
.