Why not accept field creator (creator_id)?

There is a model.
model.py
class 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 = 'Patient'
 verbose_name_plural = "Patients"


forms.py
class form_patient(forms.ModelForm):
 last_name = forms.CharField(label='last Name', widget=forms.TextInput(
 attrs={'class': 'form-control'}
))
 first_name = forms.CharField(label='Name', widget=forms.TextInput(
 attrs={'class': 'form-control'}
))
 patronymic = forms.CharField(label='middle name', widget=forms.TextInput(
 attrs={'class': 'form-control'}
))
 birthday = forms.DateField(label='date of birth', widget=forms.DateInput(
 attrs={'class': 'form-control', 'type': 'date'}
))
 gender = forms.ModelChoiceField(label='Gender', queryset=gender.objects.all(), widget=forms.Select(
 attrs={'class': 'form-control'}
))
 creator = forms.ModelChoiceField(label='Creator', 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.py
from 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())


These are the data I get:
<querydict: {'first_name': ['Victor'], 'birthday': ['1964-12-23'], 'csrfmiddlewaretoken': ['wzun4ninjxbjnoda8cxqgpjdw0qcihrmvhoyujbpegkuhqp8uwkm6ddudtqhyucq'], 'create_date': ["], 'creator': ['1'], 'gender': ['2'], 'patronymic': ['ye'], 'create_time': ['12:10'], 'last_name': ['Kadushin']}></querydict:>


And gives me this error.
(1048, "Column 'creator_id' cannot be null")
June 7th 19 at 15:03
2 answers
June 7th 19 at 15:05
1. The class name should be capitalized
2. For DateField does not need max_length=150
3. discover DateTimeField
and so.. your code hard to read

And on the issue, usually a creator generally exclude from the form and added when saving after form.is_valid()
PS, here's another notice in the form fields = ['last_name', 'first_name', 'patronymic', 'birthday', 'gender'] so perhaps because of this, and ignored the creator.
well, even considering your edit - the result is the same. - Millie_Bechtel commented on June 7th 19 at 15:08
what changes have you considered? - Garnett_Deckow commented on June 7th 19 at 15:11
plus read the how to change widget from ModelForm https://docs.djangoproject.com/en/2.0/topics/forms... - Garnett_Deckow commented on June 7th 19 at 15:14
, rolled back as it was and restarted the server - worked. - Millie_Bechtel commented on June 7th 19 at 15:17
June 7th 19 at 15:07
You have to watch how you have populated this field in the database. It is mandatory you do the same for some reason is empty.
creator on models creator_id in the database through the model reassigned the name, as in the documentation. - Millie_Bechtel commented on June 7th 19 at 15:10

Find more questions by tags PythonDjango