djangodjango-class-based-views

Multiple models in UpdateView


Is it possible to pass multiple models into the UpdateView?

Something like:

models = (FirstModel, SecondModel)

Solution

  • Not via the models attribute for UpdateView.

    But what you can do is either utilize extra_context or override the get_context_data() and add the models there.

    An example of one such override would be:

    class TaffyUpdateView(UpdateView):
    
        def get_context_data(self, **kwargs):
            context = super(TaffyUpdateView, self).get_context_data(**kwargs)
            context['second_model'] = SecondModel.objects.get(id=1) #whatever you would like
            return context