E
E
Egor Vavilov2020-04-25 23:37:55
Django
Egor Vavilov, 2020-04-25 23:37:55

How to connect CreateView and UpdateView?

I have a custom form. It has two dynamic Select Boxes. The content of one depends on the URL, the second - on the first through an API request. In order to form the form, I got a large CreateView class. In it, for example, the URL is processed (dispatch), auxiliary data is added to the data incoming from the form before being written to the database (form_valid), etc.

class CostCreate(CreateView):
        def dispatch(self, request, *args, **kwargs):
            try:
                self.type_of_cost = kwargs['type_of_cost']
            except KeyError:
                self.type_of_cost = 'daily'

        def form_valid(self, form):
            f = form.save(commit=False)
            f.type = self.type_of_cost
            f.save()
            return super().form_valid(form)


Naturally, all these methods must be done for UpdateView, so it turns out to be a duplicate of the first class in many ways.

When I look at it, I understand that these two classes need to be merged. But I don't understand exactly how. Tell me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pan Propan, 2020-04-25
@Shecspi

Through mixin

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question