好吧,我有一个字段类型FileField,当我尝试删除它的内容时,返回给我这个错误信息:'bool'对象没有属性'_committed'` 要正常上传,只有在我尝试删除已发送的文件时才会出现问题。
我的观点:
try:
profile = request.user.get_profile()
company = profile.company
except ObjectDoesNotExist:
raise Http404
if request.method == 'POST':
data = request.POST
profile_form = EditUserProfileForm(data=data)
company_form = EditCompanyForm(data=data, files=request.FILES)
if profile_form.is_valid() and company_form.is_valid():
profile_form.save(profile)
company_form.save(company)
messages.info(request, 'Dados atualizados com sucesso.')
return HttpResponseRedirect('')
else:
profile_form.save(profile)
company_form.save(company) #Error here.
messages.info(request, 'Dados atualizados com sucesso.')
return HttpResponseRedirect('')
编辑:完整的追溯
Traceback:
File "/home/vanderson/.virtualenvs/cargobr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111.response = callback(request, *callback_args, **callback_kwargs)
File "/home/vanderson/.virtualenvs/cargobr/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/home/vanderson/Sites/cargo-br/cargobr/../cargobr/apps/accounts/views.py" in edit
407. company_form.save(company)
File "/home/vanderson/Sites/cargo-br/cargobr/../cargobr/apps/accounts/forms.py" in save
147. company.save()
File "/home/vanderson/.virtualenvs/cargobr/local/lib/python2.7/site-packages/django/db/models/base.py" in save
460. self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/home/vanderson/.virtualenvs/cargobr/local/lib/python2.7/site-packages/django/db/models/base.py" in save_base
484. signals.pre_save.send(sender=origin, instance=self, raw=raw, using=using)
File "/home/vanderson/.virtualenvs/cargobr/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py" in send
172. response = receiver(signal=self, sender=sender, **named)
File "/home/vanderson/.virtualenvs/cargobr/local/lib/python2.7/site-packages/easy_thumbnails/signal_handlers.py" in find_uncommitted_filefields
20. if not getattr(instance, field.name)._committed:
Exception Type: AttributeError at /editar-cadastro/
Exception Value: 'bool' object has no attribute '_committed'
编辑:EditCompanyForm:
class EditCompanyForm(CompanyForm):
class Meta:
model = accounts.models.Company
exclude = ('verified', 'rating')
def save(self, company):
company.insurance_policy = self.cleaned_data['insurance_policy']
company.save()
return company
答案 0 :(得分:2)
这种回应来得很晚,但可能会帮助其他人,因为今天我遇到了类似的问题。我在views.py的同一行上收到了相同的错误消息,每当我想保存一个空的ImageField时都会发生。
我的观点:
def recipe_new(request):
if request.method == 'POST':
form = RecipeForm(request.POST, request.FILES or None)
if form.is_valid():
recipe = form.save(commit=False)
recipe.author = request.user
recipe.published_date = timezone.now()
recipe.save() #Error here.
return redirect('recipe_detail', pk=recipe.pk)
else:
form = RecipeForm()
return render(request, 'blog/recipe_edit.html', {'form': form})
我的表单:
class RecipeForm(forms.ModelForm):
class Meta:
model = Recipe
fields = ('title', 'image', 'is_vegan', 'is_veggie', 'cuisine',
'contained_allergen', 'instruction')
我的模特:
class Recipe(models.Model):
author = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
image = models.ImageField(upload_to="images", default=True)
is_vegan = models.BooleanField("Vegan", default=False)
is_veggie = models.BooleanField("Vegetarisch", default=False)
etc.
事实证明ImageField中的参数default=True
引起了问题。删除后,错误消息消失了。
答案 1 :(得分:1)
我有同样的错误,原因是我没有在我的表单中设置enctype,
<form enctype="multipart/form-data" method="post" action="/foo/">