Django : use @property in success_url KO . while using it with get_success_url is OK (class based view)

I found a workaround for my issue but I need to know why the first above case doesn't work.

I need to pass a parameter (reman_pk) to my view but when I try :

class RepairCreateView(LoginRequiredMixin, CreateView):

    @property
    def reman_pk(self):
        return int(self.kwargs['reman_pk'])
    [...]
    success_url = reverse_lazy(
        'reman:update-reman', kwargs={'pk': reman_pk})
    [...]

... I got an error

django.urls.exceptions.NoReverseMatch: Reverse for 'update-reman' with keyword arguments '{'pk': <property object at 0x10c20bbd0>}' not found. 1 pattern(s) tried: ['reman/update/(?P[0-9]+)/$']

But when in the same class based view I use :

def get_success_url(self, **kwargs):
        if kwargs != None:
            return reverse_lazy('reman:update-reman', kwargs={'pk': self.reman_pk})

... it's OK : an int is well passed in my URL.

I tried to pass int(reman_pk) in the first method ... not better.

I've already use @property in the past and always got a value (int/str) and not property object.

This is not related to django, this is related to python in general. When you want to access a class property within the class you always have to call self before!

class Tree:
    fruits = 5

    @property
    def leafes(self):
        return self.fruits * 5

    def show_tree(self):
        print(self.fruits)
        print(self.leafes)
        print(leafes)  # THIS LINE WOULD ERROR
Back to Top