7

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

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
smarber
  • 4,829
  • 7
  • 37
  • 78

1 Answers1

9

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.

mariodev
  • 13,928
  • 3
  • 49
  • 61
  • But if object was created already, why isn't it available for my new method post? What I meant is that get_context_data is obviously called first, then self.object is created and saved, so logically it should be available for any method called after get_context_data into the instance of my class, unless the method post is called into another instance, or self.object is cleared before..Does what I said make sense? – smarber Oct 05 '13 at 13:01
  • 2
    @smarber No no.. if you look at the DetailView source code, `get_context_data` is actually called inside `get` method after the object has been created (inside `get` as well). So you need to do the same thing, when you decide to create your own method (post, put, delete, etc..) you need to grab the object by yourself. Have you looked at ccbv.co.uk reference? – mariodev Oct 05 '13 at 13:13