Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 639
smarber Avatar asked Oct 05 '13 11:10

smarber


1 Answers

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.

like image 118
mariodev Avatar answered Sep 29 '22 17:09

mariodev