python - Django form_valid is not working -
i new django . can me code. trying calculate duration between 2 datefield save.
class employeecreate(createview): model = employee form_class = employeecreateform success_url = "/employee-list/" def form_valid(self, form): self.object.total_leave = (self.object.to_date - self.object.from_date).days +1 self.object.save() return httpresponseredirect(self.get_success_url())
there couple of issues
- you should use
form.instance
instead ofself.object
- call super method
so update code as:
def form_valid(self, form): self.instance.total_leave = (self.instance.to_date - self.instance.from_date).days +1 self.instance.save() return super(employeecreate, self).form_valid(form)
refer docs form handling class-based views
Comments
Post a Comment