django: DetailView: self.object raises an error when called from the method post, but it does work when called from the method get_context_data

StackOverflow https://stackoverflow.com/questions/19197480

Вопрос

I created a class that inherits from DetailView, and I overrided the methods get_context_data and post. What it seems weird as mentioned in the title, is that I can call self.object from get_context_data but I can't from post so I had to use self.get_object() instead. But I would like to understand why? is self.object removed by get_context_data? so that any method called after it cannot use it or something like that?

Thanks in advance

Это было полезно?

Решение

post method doesn't exist by default in DetailView, you actually creating it not overriding, so therefore you need to fetch the object by yourself, the reason it'a available in get_context_data is that it's already fetched inside get method and saved in object property.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top