factory_boy icon indicating copy to clipboard operation
factory_boy copied to clipboard

create() method doesn't save model

Open ramusus opened this issue 13 years ago • 5 comments

I have factory

import factory
class GroupFactory(factory.Factory):
    FACTORY_FOR = Group    
    remote_id = factory.Sequence(lambda n: n)

Method create doesn't save model, I should call it especially:

In [1]: from vkontakte_groups.factories import GroupFactory    
In [2]: group = GroupFactory.create()    
In [3]: group.id, group.pk
Out[3]: (None, None)    

In [4]: group.save()    
In [5]: group.id, group.pk
Out[5]: (890, 890)    

In [6]: import factory    
In [7]: factory.__version__
Out[7]: '2.1.0-dev'

That is very strange. Before it was behaviour with saving by default and I don't know what was changed.. Should I post here more environment settings?

ramusus avatar May 07 '13 20:05 ramusus

The ".create() calls save()" behavior changed from version 2.0.0 ; it was deprecated since version 1.3.0 (including deprecation warnings ; see the ChangeLog).

For Django factories, you should inherit from factory.DjangoModelFactory; it provides the following benefits:

  • Automatic call to .create()
  • Ability to use some fields for a get_or_create call
  • Automatic save() after post-generation hooks ran

More details on the DjangoModelFactory documentation

rbarrois avatar May 07 '13 22:05 rbarrois

Thanks. It's completely solve my issue

ramusus avatar May 08 '13 22:05 ramusus

Could we document the ORM Factories in the README in like a section called Supported ORMs or something to make it more obvious we should use a different factory subclass? This took a little while to figure out and is a pretty fundamental change from 1.3.

erickyellott avatar May 22 '13 03:05 erickyellott

@yellottyellott That's a very good point, I'll do it right away.

rbarrois avatar May 22 '13 05:05 rbarrois

XD!

erickyellott avatar May 22 '13 05:05 erickyellott