Reputation: 3
I've been trying for over a week now to save a new model object that has one ManyToMany field using save(commit=False) but I keep running into the same error. I've tried over 10 different approaches and haven't been able to resolve this. Thanks in advance for your help! See relevant code below:
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)
Upvotes: 0
Views: 1360
Reputation: 3755
On this:
vquestions = models.ManyToManyField(Questions, blank = True, null = True, choices = VID_QUESTIONS)
Try using limit_choices_to
(docs) instead of defining a set of choices with choices =
. This can be done on the model and will impact the available choices on all modelforms where that model is used. That also means you don't have to define a queryset in forms.py; you can pass an additional parameter to limit_choices_to
to only allow associations with approved questions.
Since you are using commit = False
with a form that touches a many-to-many field, I believe you will also need to use save_m2m (docs) in order to get things to save properly.
Upvotes: 1