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

Invoking logic from model's .save() method #42

Closed
pahaz opened this issue Nov 6, 2016 · 5 comments
Closed

Invoking logic from model's .save() method #42

pahaz opened this issue Nov 6, 2016 · 5 comments

Comments

@pahaz
Copy link

pahaz commented Nov 6, 2016

Hi! I have peewee model with custom save method:

        def save(self, *args, **kwargs):
            if not self.slug:
                self.slug = slugify(self.title.lower())
            return super().save(*args, **kwargs)

And If I call await self.manager.create(...) the save method will not call.
It may little confuse.

@rudyryk
Copy link
Member

rudyryk commented Nov 11, 2016

Yes, there's no way to call save() asynchronously. I agree it's not obvious and should be highlighted in docs. Thank you for the feedback!

@gwillem
Copy link

gwillem commented Dec 14, 2016

Not sure where I should put my save() auto validation/transformation code then... perhaps in a model's _transform_mutations() method, that is called by an overloaded insert() and update() ?

PS. @rudyryk thanks for making this awesome library! It was a pleasure to replace all threadpool executor calls in my codebase :)

@rudyryk
Copy link
Member

rudyryk commented Mar 5, 2017

@gwillem Thank you :)

I think it's up to application code design. I prefer to leave that code in Manager subclasses and have some domain specific methods, so we can have an extra abstraction layer above models. That may be good for separation into multiple services in future.

But in fact, async code is usually more verbose for now and many design decisions still need to be made for better code.

Here's an example:

class PostManager(peewee_async.Manager):
    async def create_for_user(self, user, **attributes):
         """Save post by user."""
         # ...

    async def create_from_rss(self, **rss_data):
         """Save post imported from RSS."""
         # ...

@gwillem
Copy link

gwillem commented May 1, 2017

I use this, which has generic validation if a model has a validate method:

class ValidationManager(peewee_async.Manager):

    @asyncio.coroutine
    def update(self, obj, **kwargs):
        if hasattr(obj, 'validate'):
            obj.validate()
        return super().update(obj, **kwargs)

    @asyncio.coroutine
    def create(self, model_, **data):
        """WdG: overloaded, because we can run validate()
        """
        inst = model_(**data)

        if hasattr(inst, 'validate'):
            inst.validate()

        query = model_.insert(**dict(inst._data))

        pk = yield from self.execute(query)
        if pk is None:
            pk = inst._get_pk_value()
        inst._set_pk_value(pk)

        inst._prepare_instance()
        return inst

However, now I had to copy the create method. Hmm, I'd better pin my dependencies :D

@rudyryk
Copy link
Member

rudyryk commented Aug 5, 2018

This is probably related to #96

@rudyryk rudyryk changed the title The .save() method is not call Invoking logic from model's .save() method Aug 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants