我已经尝试了一个多星期来保存一个新的模型对象,该对象有一个使用save(commit = False)的ManyToMany字段,但我一直遇到同样的错误。我已经尝试过10种不同的方法并且无法解决这个问题。在此先感谢您的帮助!请参阅以下相关代码:
DatabaseError at /createRezidio/ (1364, "Field 'vquestions' doesn't have a default value")
['/Users/matthewbaron/Desktop/Nonsense/Loeb Land/source/commapp',
'/Library/Python/2.7/site-packages/virtualenv-1.9.1-py2.7.egg',
'/Library/Python/2.7/site-packages/setuptools-5.4.1-py2.7.egg',
'/Library/Python/2.7/site-packages/distribute-0.6.28-py2.7.egg',
'/Library/Python/2.7/site-packages/MySQL_python-1.2.4-py2.7-macosx-10.9-intel.egg',
'/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
'/Library/Python/2.7/site-packages',
'/Library/Python/2.7/site-packages/PIL']
models.py
class Questions(models.Model):
userId = models.ForeignKey(User)
question = models.TextField()
approved = models.BooleanField(default=False)
def __unicode__(self):
return "%s" % (self.question)
class Profiles(models.Model):
userId = models.ForeignKey(User)
university = models.CharField(max_length = 60)
universityYear = models.CharField(max_length = 60, choices=GRADE_CHOICES)
gpa = models.DecimalField(max_digits = 4, decimal_places = 3, validators= [MinValueValidator(0), MaxValueValidator(5)])
major = models.CharField(max_length = 60)
resumeText = models.TextField(help_text = 'Paste text of resume here:')
vquestions = models.ManyToManyField(Questions, blank = True, null = True, choices = VID_QUESTIONS)
resumeFile = models.FileField(upload_to = file_name_resume)
video1 = models.FileField(upload_to = file_name_video1)
video2 = models.FileField(upload_to = file_name_video2)
video3 = models.FileField(upload_to = file_name_video3)
video4 = models.FileField(upload_to = file_name_video4)
userPhoto = models.ImageField(upload_to = file_name_photo)
def __unicode__(self):
return "%s, %s, %s, %s, %s, %s,
views.py
def createRezidio(request):
if request.method == 'POST':
form = AutoRezidioForm(request.POST, request.FILES)
if form.is_valid():
newprof = form.save(commit=False)
newprof.userId = request.user
newprof.save()
return HttpResponseRedirect("/viewRezidio/")
else:
form = AutoRezidioForm() # A empty, unbound form
if request.user.groups.filter(name="recruiter").count() > 0:
return render_to_response(
"users/base_rezidio.html",
{'form': form, 'type' : 'recruiter'},
context_instance=RequestContext(request)
)
return render_to_response(
"users/base_rezidio.html",
{'form': form, 'type' : 'student'},
context_instance=RequestContext(request)
)
forms.py
class AutoRezidioForm(ModelForm):
class Meta:
model = Profiles
exclude = ['userId']
vquestions = forms.ModelMultipleChoiceField(queryset = Questions.objects.all().filter(approved = True), widget=SelectMultiple)
答案 0 :(得分:0)
关于这个:
vquestions = models.ManyToManyField(Questions, blank = True, null = True, choices = VID_QUESTIONS)
尝试使用limit_choices_to
(docs),而不是使用choices =
定义一组选项。这可以在模型上完成,并将影响使用该模型的所有模型的可用选择。这也意味着您不必在forms.py中定义查询集;您可以将其他参数传递给limit_choices_to
,以仅允许与已批准的问题相关联。
由于您使用的commit = False
表单触及多对多字段,因此我相信您还需要使用save_m2m(docs)才能正确保存。