I create a verification of users. In the form must be only two fields with photos. The user pulls up automatically, the comment is set in the admin panel. When saving a form, an error flies. IntegrityError at / identification /
ОШИБКА: нулевое значение в столбце "user_id" нарушает ограничение NOT NULL DETAIL: Ошибочная строка содержит (11, accounts/request_user/images/4c2c3ea4-a806-4964-9f1e-2e90dc91deb..., abc/Снимок_экрана_от_2019-02-21_15-10-05_LsQ3IE4.p..., , f, 2019-03-06 13:21:09.453797+00, null, 2019-03-06 13:21:09.453831+00).
models
class RequestUser(models.Model): user = models.OneToOneField(User) main_photo = models.ImageField(upload_to=get_file_path) profile_photo = models.ImageField(upload_to="abc") comment = models.CharField(max_length=40, default='') is_verified = models.BooleanField(default=False) created_at = models.DateTimeField(_('created at'), auto_now_add=True) updated_at = models.DateTimeField(_('updated at'), auto_now=True) @property def upload_dir(self): return 'accounts/request_user/images' def __unicode__(self): return self.user forms
class IdentificationForm(forms.ModelForm): class Meta: model = RequestUser fields = ('user', 'main_photo', 'profile_photo', 'comment') comment = forms.CharField( widget=forms.HiddenInput(), required=False) user = forms.CharField( widget=forms.HiddenInput(), required=False) views
def verification_view(request): if request.method == 'POST': form = IdentificationForm(request.POST, request.FILES) if form.is_valid(): main_photo = form.cleaned_data['main_photo'] user = request.user comment = form.cleaned_data['comment'] profile_photo = form.cleaned_data['profile_photo'] form.save() return HttpResponse('image upload success') else: form = IdentificationForm() return render(request, 'accounts/identification.html', {'form': form}) template
<form action="" class="edit_form new_event" enctype="multipart/form-data" method="POST"> {% csrf_token %} {% for error in form.non_field_errors %} {{error}} {% endfor %} {{ form.user }} {{ form.comment }} {{ form.main_photo }} {{ form.profile_photo }} <input type="submit" class="submit mgln" value="Послать запрос"> </form>