Django - empty form cannot be saved in database

浪尽此生 提交于 2020-01-25 07:49:05

问题


I have a form in which, user specifies lawyer-spec and save the data to the database. However I get an error that

**null value in column "lawyer_spec" violates not-null constraint**

So the data from the form is not processed properly.

EDIT:

mthod form_invalid prints empty line, and then two numbers ('pk' and 'lawyer_id')

forms.py

class MakeAppointmentForm(forms.ModelForm):
    first_name = forms.CharField(required=True)

    class Meta:
        model = CalendarAppointmentsReserved
        fields = ['case']

    def __init__(self, *args, lawyer_id, pk, **kwargs):
        super().__init__(*args, **kwargs)
        lawyer_id_from_kwargs = lawyer_id
        lawyer_specs = LawyersSpec.objects.filter(lawyer=lawyer_id_from_kwargs)

        choices = [(spec.lawyer_spec, dict(CASES)[spec.lawyer_spec]) for spec in lawyer_specs]

        self.fields['case'].choices = choices

views.py

@method_decorator(user_required, name='dispatch')
class MakingAppointmentView(CreateView):
    template_name = "make_appointment.html"
    form_class = TestPy.forms.MakeAppointmentForm
    model = TestPy.models.CalendarAppointmentsReserved

    def get_form_kwargs(self):
        pk = self.kwargs.get('pk')
        self.kwargs = super().get_form_kwargs()

        self.kwargs = {'lawyer_id': self.request.session['lawyer_id'], 'pk': pk}

        self.kwargs.update(self.kwargs)  # self.kwargs contains all url conf params
        return self.kwargs


    def form_invalid(self, form):
        print(form.errors)
        print(self.kwargs.get('pk'))
        print(self.kwargs.get('lawyer_id'))
        return redirect('home')


    def form_valid(self, form):
        calendar_model = TestPy.models.CalendarFreeSlot.objects.get(pk=self.kwargs.get('pk'))
        calendar_model.is_available = False
        calendar_model.save()

        self.object = form.save(commit=False)
        self.object.users_id = self.request.user
        self.object.calendar_free_id = calendar_model
        self.request.session['free_calendar_id'] = calendar_model.pk
        self.request.session['lawyer_id'] = calendar_model.lawyer_id.pk

        self.object.save()
        return redirect('home')

models.py

class LawyersSpec(models.Model):
    lawyer = models.ForeignKey('MyUser', on_delete=models.PROTECT)
    lawyer_spec = models.SmallIntegerField(choices=CASES)

class CalendarAppointmentsReserved(models.Model):
    calendar_free_id = models.ForeignKey('CalendarFreeSlot', on_delete=models.PROTECT) 
    users_id = models.ForeignKey('MyUser', on_delete=models.PROTECT) 
    case = models.SmallIntegerField(choices=CASES)

How can I process the data properly and save in the database?


回答1:


You're making a crucial mistake here:

  • Your CreateView is for a CalendarAppointmentsReserved model
  • Your form is for a LawyersSpec model

That's not possible, because the create view for model A is expecting a ModelForm for the same model A.

Change your form to this:

class MakeAppointmentForm(forms.ModelForm):
    first_name = forms.CharField(required=True)

    class Meta:
        model = CalendarAppointmentsReserved
        fields = ['case']

    def __init__(self, *args, lawyer_id, pk, **kwargs):
        super().__init__(*args, **kwargs)
        lawyer_specs = LawyersSpec.objects.filter(lawyer=lawyer_id)
        choices = [(spec.lawyer_spec, dict(CASES)[spec.lawyer_spec]) for spec in lawyer_specs]
        self.fields['case'].choices = choices

Now in your view you don't need to assign self.object.case anymore.

You're also resetting self.kwargs in your get_form_kwargs() method by assigning it a dictionary directly. And then self.kwargs.update(self.kwargs) does nothing, you're updating a dict with itself. So you loose all the POST data here. The proper way to do it is like this:

def get_form_kwargs(self):
    form_kwargs = super().get_form_kwargs()  # don't mess up self.kwargs
    pk = self.kwargs.get('pk')
    form_kwargs.update({'lawyer_id': self.request.session['lawyer_id'], 'pk': pk})
    return form_kwargs



来源:https://stackoverflow.com/questions/57711611/django-empty-form-cannot-be-saved-in-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!