Answer the question
In order to leave comments, you need to log in
How to properly structure a Model in Django?
Hello everyone once again.
I read the documentation on the Models, studied everything, but there were questions. I hope you can advise.
Abstract situation:
1. There is a Model with several fields.
2. I need to write a number of internal functions for the model, so that the code would be more understandable, structured in more detail and the main load on working with the model fell not on the CWB, but on the internal functions of the model.
Actually, a question:
1. There is a function in model on delivery of the partial information (several fields of record).
#к примеру, простая функция внутри модели:
def return_user_age(self, user):
...
return user.age
print(Users.return_user_age(id=3))
Answer the question
In order to leave comments, you need to log in
If based on the option in the question, then so:
@classmethod
def return_user_age(cls, user_id):
user = cls.objects.get(id=user_id)
return user.age
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question