How is the super method to be used in Django?

Viewed 159

I don't want you to think I would be lazy. I already read about the super method to override pre-existing methods in Django (and possibly also in Python in general). Still I must admit that I did not really understand everything.

So, instead of asking for a particular application of the method, I wanted to ask how this method can be applied. Sounds weird or too broad? Perhaps it helps, if I describe my problem.

Let's say I want to enhance a class-based view. My experience with them is also limited. Very often, pages on the web just say, "yeah, you have to override this or that method." Me, "Ehm, how do I actually know about the existence of this or that method?" So far, I have to come to know a few like get, post and form_valid, but I do not know in general which are available or where I have to read them up, as I guess that they are in subclasses of subclasses of subclasses in some general files and I don't even know where those are.

So, imagine I have found the corresponding method, then I do not know how to handle that. Here is an example. I wanted to customize an update view so that it also works if I call it from a button within a post method form.

class Update_Org(UpdateView):
    model = OrgKlass
    form_class = form_update_org
    template_name = templ_folder_name + 'update_org.html'
    success_url = reverse_lazy(url_app_name + 'list_org')

    def get_object(self, queryset=None):
        self.queryset = OrgKlass.objects.get(id = self.to_update)
        return self.queryset

    def post(self, request, pk):
        if self.request.POST.get('action', False) == 'update':
            self.to_update = pk
            self.object = self.get_object()
            return super(Update_Org, self).post(request, pk)
        else:
            [...]

I thought that this would override the post method, that I would retrieve the corresponding object with self.get_object() (which seems to work) and that this would prepopulate the form, but it does not. How can I learn to find the right way to do such things?

Sorry, if my question is still too broad.

0 Answers
Related