I want to add a new function to the default User model of Django for retrieveing a related list of Model type.
Such Foo model:
class Foo(models.Model):
owner = models.ForeignKey(User, related_name="owner")
likes = models.ForeignKey(User, related_name="likes")
........
#at some view
user = request.user
foos= user.get_related_foo_models()
How can this be achieved?
You can add a method to the User
from django.contrib import auth
auth.models.User.add_to_class('get_related_foo_models', get_related_foo_models)
Make sure, you have this code within the models.py or some other file which gets imported in the startup of django.