Есть модель.
model.pyclass patient(models.Model):
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
patronymic = models.CharField(max_length=150)
birthday = models.DateField(max_length=150)
gender = models.ForeignKey(gender, default=None, null=True, on_delete=models.CASCADE)
creator = models.ForeignKey(User, related_name='creator', on_delete=models.CASCADE)
create_date = models.DateField()
create_time = models.TimeField()
editor = models.ForeignKey(User, related_name='editor', default=None, null=True, on_delete=models.CASCADE)
edit_date = models.DateField(default=None, null=True)
edit_time = models.TimeField(default=None, null=True)
def __str__(self):
return '{0} {1} {2}'.format(self.last_name, self.first_name, self.patronymic)
class Meta:
verbose_name = 'Пациент'
verbose_name_plural = "Пациенты"
forms.pyclass form_patient(forms.ModelForm):
last_name = forms.CharField(label='Фамилия', widget=forms.TextInput(
attrs={'class': 'form-control'}
))
first_name = forms.CharField(label='Имя', widget=forms.TextInput(
attrs={'class': 'form-control'}
))
patronymic = forms.CharField(label='Отчество', widget=forms.TextInput(
attrs={'class': 'form-control'}
))
birthday = forms.DateField(label='Дата рождения', widget=forms.DateInput(
attrs={'class': 'form-control', 'type': 'date'}
))
gender = forms.ModelChoiceField(label='Пол', queryset=gender.objects.all(), widget=forms.Select(
attrs={'class': 'form-control'}
))
creator = forms.ModelChoiceField(label='Создатель', queryset=User.objects.all(), widget=forms.HiddenInput(
attrs={'class': 'form-control'}
))
create_date = forms.DateField(widget=forms.HiddenInput, required=False)
create_time = forms.TimeField(widget=forms.HiddenInput, required=False)
class Meta:
model = patient
fields = ['last_name', 'first_name', 'patronymic', 'birthday', 'gender']
views.pyfrom django.shortcuts import render
from PATIENT.forms import *
import datetime
# Create your views here.
def patient(request):
if request.user.is_authenticated:
patient_form = form_patient(request.POST or None,
initial={
'creator': request.user,
'create_time': format(datetime.datetime.now(), '%Y-%m-%d'),
'create_time': format(datetime.datetime.now(), '%H:%M')
})
print(patient_form.data)
if request.POST and patient_form.is_valid():
patient_form.save()
return render(request, 'patient/patient.html', locals())
else:
return render(request, 'staff/login.html', locals())
Вот такие данные я получаю:
<QueryDict: {'first_name': ['Виктор'], 'birthday': ['1964-12-23'], 'csrfmiddlewaretoken': ['wZUn4NINjXbJNOdA8cXQgPJDW0qCihRmVHoyUjbpEgkUHqp8uwkm6DduDTQHYUCq'], 'create_date': [''], 'creator': ['1'], 'gender': ['2'], 'patronymic': ['Егорович'], 'create_time': ['12:10'], 'last_name': ['Катюшин']}>
И мне выдает такую ошибку.
(1048, "Column 'creator_id' cannot be null")