I was wondering which is the correct way to use models(ORM) of mongonegine python.
I have a coupon class which directly maps to mongodb json document.
class Coupon(Document):
__collection__ = 'coupon'
coupon = StringField()
points = IntField()
@property
def increment(self):
self.points += 1
@property
def decrement(self):
self.points += 1
The thing that i find wrong with above is , we are binding behaviour to database access layer, which i think is not correct way of doing things (I might be completely wrong).
Another way (Which I think makes more sense) is delegating business logic to some other layer commonly called as service layer:
models.py
class Coupon(Document):
__collection__ = 'coupon'
coupon = StringField()
points = IntField()
services.py
from models import Coupon
def increment_points(c_id):
c_id.points+=1
I want to know which is the correct way of doing things(assuming we have lots of models).
Question: should we bind behaviors to models or use a service layer for business logic?
NOTE: I have added tag python
because I have seen many people binding behaviours and business logic to the DB access layer in their codebases.services are rarely heard of in any of the tutorials about python web frameworks. but its very common in Java and PHP.