I am writing a project in Django and I see that 80% of the code is in the file models.py
. This code is confusing and, after a certain time, I cease to understand what is really happening.
Here is what bothers me:
User
, but technically it should create them uniformly.Here is a simple example. At first, the User
model was like this:
class User(db.Models):
def get_present_name(self):
return self.name or 'Anonymous'
def activate(self):
self.status = 'activated'
self.save()
Over time, it turned into this:
class User(db.Models):
def get_present_name(self):
# property became non-deterministic in terms of database
# data is taken from another service by api
return remote_api.request_user_name(self.uid) or 'Anonymous'
def activate(self):
# method now has a side effect (send message to user)
self.status = 'activated'
self.save()
send_mail('Your account is activated!', '…', [self.email])
What I want is to separate entities in my code:
What are the good practices to implement such an approach that can be applied in Django?