Search code examples
djangodjango-modelsdjango-templatesextend

How can I add a custom method to auth_user model class? I need to return a calculated field along with results


I want to be able to write something like:

#extend user model with this method
def get_manager():
    #call some api to get the manager
    return user_manager

and to be able to display this in the template, like:

{{ user.get_manager }}

Please note that manager here is not the class manager. Actually my users are employees and I need to return the manager of each employee with the result set


Solution

  • You can extend the django user model.

    class Employee(models.Model):
        user = models.OneToOneField(User)
        def get_manager():
            ...
    

    and in your template you can write

    {{ user.employee.get_manager }}