Skip to content
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

Closed
coleifer opened this issue Apr 24, 2015 · 3 comments
Closed

Support for Peewee ORM? #200

coleifer opened this issue Apr 24, 2015 · 3 comments
Labels

Comments

@coleifer
Copy link

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!

@rbarrois
Copy link
Member

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?

@coleifer
Copy link
Author

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!

@Seluj78
Copy link

Seluj78 commented Sep 25, 2024

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 factory_boy with peewee:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants