所以我有一个用户模型一对一的字段有两个文件字段,我上传这两个文件没有问题,但是当我尝试更改文件时,我得到了这个:
model.py :
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
demand_file = models.FileField(upload_to='archivos', blank=True, validators=[validate_file_extension])
order_file = models.FileField(upload_to='archivos', blank=True, validators=[validate_file_extension])
view.py
@login_required
def upload_files(request):
if request.method == 'POST':
form = UserFileUploadForm(request.POST, request.FILES)
if form.is_valid():
profile = form.save(commit=False)
profile.user = request.user
profile.save()
return HttpResponseRedirect(reverse('index'))
else:
messages.info(request, 'Archivo no valido, por favor ingrese un archivo CSV')
return render(request, 'project_app/file_upload.html', {'uploads': form})
else:
form = UserFileUploadForm()
return render(request, 'project_app/file_upload.html', {'uploads': form})
forms.py
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())
class Meta:
model = User
fields = ('username', 'password')
class UserFileUploadForm(forms.ModelForm):
class Meta:
model = Profile
fields = ('demand_file', 'order_file')
在控制台中我得到了
django.db.utils.IntegrityError:NOT NULL约束失败:project_app_profile.user_id
所以我做错了什么,只是看不出来。 无论如何,感谢您提供的任何帮助。