Django CBV FormView get_intial form_valid没有保存,但它用def post保存

时间:2015-06-26 07:02:24

标签: python django django-class-based-views

class EditShipment(LoginRequiredMixin, FormView):
    template_name = "add-edit-shipment.html"
    model = Shipment
    form_class = ShipmentForm
    success_url = lazy(reverse,str)("shipment_listing")

    def get_context_data(self, **kwargs):
        context = super(EditShipment,self).get_context_data(**kwargs)
        context['shpid'] = self.kwargs.get('shpid',None)
        context['key'] = 'edit'
        return context

    def get_initial(self):
        ship_obj = Shipment.objects.get(id=int(self.kwargs.get('shpid',None)))
        stdr_obj = Shipment_Truck_Driver_Relation.objects.get(shipment = ship_obj)
        return {'origin_address':ship_obj.origin_address,\
                'destination_address':ship_obj.destination_address,\
                'total_weight':ship_obj.total_weight,'pick_up':ship_obj.pick_up,\
                'delivery':ship_obj.delivery,'amount':ship_obj.amount,\
                'truck':stdr_obj.truck,'driver':stdr_obj.driver}


    def get_form(self, form_class):
        return form_class(initial=self.get_initial())

    def form_valid(self,form):
        import ipdb;ipdb.set_trace()
        userprofile_obj = User_Profile.objects.get(user = self.request.user)
        compuser_obj = Company_Users.objects.get(user_profile = userprofile_obj)
        self.f = form.save(commit=False)
        self.f.total_weight = self.request.POST.get('total_weight')
        self.f.pickup_cutoff = self.request.POST.get('pick_up')
        self.f.delivery_cutoff = self.request.POST.get('delivery')
        self.f.customer, self.f.created_by = compuser_obj.company, userprofile_obj
        self.f.save()
        return super(EditShipment, self).form_valid(form)

此处form_valid未保存,但未抛出任何错误,对象值也未更新。

如何在def form_valid中保存?

0 个答案:

没有答案