mardi 5 mai 2015

get_object_or_create in a Mixin for differents models

I have a Mixin that allow me update the objects that I have created already, thing is that I have too many models and each one with different fields, this Mixin when not found the object return a 404, I need when the object is not found return the form for create the object associated to predio_id object , I have tried with get_object_or_create, but with this method I have to pass each field. How can achieve that when the object is not found, return his corresponding empty form for create it?

class UpdateModelMixin(object):
    def get_object(self):
        return get_object_or_404(self.model,predio_id=self.kwargs['predio_id'])

and it's called to view like this one:

class ManejoGeneralUpdateView(UpdateModelMixin, UpdateView):
    model = ManejoGeneral
    form_class = FormManejoGeneral
    success_url = '/'
    template_name = 'manejo_finca/edit/manejo_general.html'

Note that the UpdateView that I wrote here is just one of almost 30 o 40 UpdateViews that I have because each UpdateView call a different form and template

Aucun commentaire:

Enregistrer un commentaire