|
|
|
|
|
by blackrobot
1817 days ago
|
|
Are there any good articles or examples you can share that elaborate on why using services is best? Writing a custom model manager method for these sorts of operations seems to work best. For instance, the create_account service could easily be part of the User.objects manager: class UserManager(models.Manager):
def create_account(self, sanitized_username: str, ...):
# the rest of the code in this method is the same as the example.
...
return user_model, auth_token
class User(models.Model):
...
objects = UserManager()
>>> User.objects.create_account(sanitized_username="blackrobot", ...)
(<User: blackrobot>, 'fake-auth-token:12345')
The benefit here is that other parts of your code only need to import the User model to access the manager methods. It also allows for the User.objects.create_account(...) method to be used by related models, without risking a circular import, by using the fk model's Model._meta.get_field(...) method.I'm not opposed to services, I just don't see when they'd be particularly useful. |
|