-
Notifications
You must be signed in to change notification settings - Fork 394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for Peewee ORM? #200
Comments
Hi @coleifer , I have currently zero knowledge of peewee, so adding support would be kinda difficult - and I wouldn't be able to maintain it. I haven't been able to design a simple and manageable way to add robust, maintained third-party compatibility in factory_boy — this would require following closely the evolution of each third-party lib. If you're interested in contributing peewee support in factory_boy, what organization would you expect/need? |
After searching around, it looks like another user has actually implemented this feature already: https://github.com/cam-stitt/factory_boy-peewee So I think it's safe to close. Thanks! |
I think it'd be interesting to re-open this issue as the problem still exists today. The repository @coleifer mentioned isn't active anymore. I currently use this code to use import peewee
from factory import base
class PeeweeOptions(base.FactoryOptions):
def _build_default_options(self):
return super(PeeweeOptions, self)._build_default_options() + [
base.OptionDefault("database", None, inherit=True),
]
class PeeweeModelFactory(base.Factory):
"""Factory for peewee models."""
_options_class = PeeweeOptions
class Meta:
abstract = True
@classmethod
def _setup_next_sequence(cls, *args, **kwargs):
"""Compute the next available PK, based on the 'pk' database field."""
model = cls._meta.model
pk = getattr(model, model._meta.primary_key.name)
max_pk = model.select(peewee.fn.Max(pk).alias("maxpk")).limit(1).order_by().execute()
max_pk = [mp.maxpk for mp in max_pk][0]
if isinstance(max_pk, int):
return max_pk + 1 if max_pk else 1
else:
return 1
@classmethod
def _create(cls, target_class, *args, **kwargs):
"""Create an instance of the model, and save it to the database."""
obj = target_class.create(**kwargs)
return obj |
Hi @rbarrois! I was curious if you had any plans to add support for peewee ORM? I believe the implementation would be quite short, as Peewee has a very simple API for creating model instances. If you do not plan on implementing it yourself, would you accept a pull-request?
Thanks again for your work!
The text was updated successfully, but these errors were encountered: