我尝试保存表单时遇到问题。希望您能够帮助我。第二件事可能是如果你有一个更好的解决方案来显示我的表格中的下拉列表。我为它构建了一个函数,但感觉非常难看。
models.py
class BBQ(models.Model):
bbq_brand = models.CharField(max_length=64, blank=True)
bbq_model = models.CharField(max_length=64, blank=True)
def __str__(self):
return self.bbq_brand
class CookSession(TimeStampedModel):
user = models.ForeignKey(User)
bbq = models.ManyToManyField(BBQ, blank=True)
cook_name = models.CharField(max_length=64)
cook_description = models.TextField()
def __str__(self):
return self.cook_name
views.py
class AddCookView(LoginRequiredMixin, FormView):
template_name = 'cooks/add_cook.html'
form_class = AddCookForm
success_url = '/cooks/'
def form_valid(self, form):
form.instance.user = self.request.user
form.save()
return super(AddCookView, self).form_valid(form)
forms.py
def get_bbq():
bbqs = []
bbq = BBQ.objects.all()
for q in bbq:
bbqs.append((q, q))
print bbqs
return bbqs
class AddCookForm(forms.ModelForm):
bbq = forms.ChoiceField(choices=get_bbq())
class Meta:
model = CookSession
exclude = ['user']
完全追踪
Environment:
Request Method: POST
Request URL: http://localhost:8000/add
Django Version: 1.9.7
Python Version: 2.7.11
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main',
'cooks',
'accounts']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/contrib/auth/mixins.py" in dispatch
56. return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/views/generic/edit.py" in post
222. return self.form_valid(form)
File "/Users/patrick/programming/django/itsmokes/src/cooks/views.py" in form_valid
21. form.save()
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/forms/models.py" in save
452. self._save_m2m()
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/forms/models.py" in _save_m2m
434. f.save_form_data(self.instance, cleaned_data[f.name])
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/db/models/fields/related.py" in save_form_data
1618. setattr(instance, self.attname, data)
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.py" in __set__
481. manager.set(value)
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.py" in set
910. self.add(*new_objs)
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.py" in add
843. self._add_items(self.source_field_name, self.target_field_name, *objs)
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.py" in _add_items
986. '%s__in' % target_field_name: new_ids,
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/db/models/query.py" in filter
790. return self._filter_or_exclude(False, *args, **kwargs)
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
808. clone.query.add_q(Q(*args, **kwargs))
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/db/models/sql/query.py" in add_q
1243. clause, _ = self._add_q(q_object, self.used_aliases)
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/db/models/sql/query.py" in _add_q
1269. allow_joins=allow_joins, split_subq=split_subq,
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/db/models/sql/query.py" in build_filter
1199. condition = lookup_class(lhs, value)
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/db/models/lookups.py" in __init__
19. self.rhs = self.get_prep_lookup()
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/db/models/fields/related_lookups.py" in get_prep_lookup
56. self.lookup_name, self.rhs)
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_lookup
746. return [self.get_prep_value(v) for v in value]
File "/Users/patrick/programming/django/itsmokes/venv/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
976. return int(value)
Exception Type: ValueError at /add
Exception Value: invalid literal for int() with base 10: 'r'