Hello, I wanted to override the get_absolute_url method in the Django User and Group models from the auth app.
My first idea was to define a proxy model, but then I noticed that the elements in usuario.groups
were instances of Group
instead of Grupo
and it also happened the same in the grupo.user_set
case. So I expanded the proxy models a bit
class Usuario(User):
class Meta:
proxy = True
@models.permalink
def get_absolute_url(self):
return ('ver_usuario', [self.id])
@property
def grupos(self):
return Grupo.objects.filter(user=self.pk)
@grupos.setter
def grupos(self, valor):
self.groups = valor
class Grupo(Group):
class Meta:
proxy = True
@models.permalink
def get_absolute_url(self):
return ('ver_grupo', [self.id])
@property
def usuarios(self):
return Usuario.objects.filter(groups=self.pk)
@usuarios.setter
def usuarios(self, valor):
self.user_set = valor
But then I saw that the RelatedManager
methods grupo.usuarios.create(username='test_usuario')
will create the user but it won't associate it with the group and this happens because grupo.user_set
is a ManyRelatedManager
and grupo.usuarios
is a QuerySet
. So maybe creating a special ManyRelatedManager
... but at this point I feel that this is growing unnecesarilly complex.
Since I want to replace get_absolute_url
I'll just use the url
tag in the templates. But I wonder, if I wanted to add other methods, how should I do it?