S
S
sandrain2014-03-27 17:54:20
Django
sandrain, 2014-03-27 17:54:20

Django request in model - how to change link?

There is the following model code:

def get_absolute_url(self):
     return reverse('vehicle-detail', kwargs={'pk': self.pk})

I would like to change this link, depending on whether the user is staff or not.
It would seem quite logical.
But I can't because request is not in the model.
What would you do?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Kuzmichev, 2014-03-27
@Assargin

I myself have not been working with Django for long, but there is an opinion that the model should not know anything about requests. Therefore, I would form the URL not in the model, but, for example, in views.py

A
Anatoly Scherbakov, 2014-03-28
@Altaisoft

Please describe in more detail why you need it.
Probably in case the user has the `is_stuff` flag set, he gets more information about the object. Maybe the pattern is different. And for this, separate pages with different URLs are made for admins and non-admins. But why? I would make it simpler: let the URL be the same for everyone, and only the final page, or rather, its view, independently determines, based on `request.user`, what to do, what to show to the user, with what template.
Otherwise, the admins will want to share the link to the page somewhere, an outsider will poke - and there 403.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question