Question

The approach of doing {% url 'view_name' object.pk object.parent.slug %} isn't really flexible when switching to completety different url patterns. I'm looking for a way to do {% url 'view_name' object %} and to transcribe myself from object to object.pk and object.parent.slug in the url.

Like that

- template.html
{% url 'view_name' object %}

- urls.py [not real regex regex]
url('<object__parent__slug>/<object__pk>', views.view_name, name="view_name")

I know this is not at all possible with this syntax, but it's just to give an idea of what I'm looking for.

Was it helpful?

Solution

I will just add url methods inside my models:

 class House(model.Models):
      name = models.TextField()
      ....

      def get_edit_url(self):
           return reverse('view_name', {'pk':self.pk, 'name':self.name, 'owner_pk' : self.owner.pk })
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top