K
K
Konstantin Malyarov2018-03-01 15:13:02
Django
Konstantin Malyarov, 2018-03-01 15:13:02

Why doesn't it accept the creator (creator_id) field?

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 = 'Пациент'
        verbose_name_plural = "Пациенты"

forms.py
class 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.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())

This is the data I get:
<QueryDict: {'first_name': ['Виктор'], 'birthday': ['1964-12-23'], 'csrfmiddlewaretoken': ['wZUn4NINjXbJNOdA8cXQgPJDW0qCihRmVHoyUjbpEgkUHqp8uwkm6DduDTQHYUCq'], 'create_date': [''], 'creator': ['1'], 'gender': ['2'], 'patronymic': ['Егорович'], 'create_time': ['12:10'], 'last_name': ['Катюшин']}>

And it gives me this error.
(1048, "Column 'creator_id' cannot be null")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
blizzard, 2018-03-01
@s41blizzard

You need to watch how you fill this field in the database. It is mandatory to fill in, but for some reason it remains empty for you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question