Django CBV - Formsets:'NoneType'对象没有属性'id'
Posted
技术标签:
【中文标题】Django CBV - Formsets:\'NoneType\'对象没有属性\'id\'【英文标题】:Django CBV - Formsets : 'NoneType' object has no attribute 'id'Django CBV - Formsets:'NoneType'对象没有属性'id' 【发布时间】:2019-02-11 17:11:39 【问题描述】:我正在使用 Django CBV,并且我正在尝试第一次使用表单集。 我想同时填写两个表单,并使用外键作为两者之间的公共元素。
我有 2 个模型:
class Publication(models.Model):
title = models.CharField(max_length=512, verbose_name=_('title'), null=False, unique=True)
description = models.TextField(verbose_name=_('description'), null=True)
download_limit = models.IntegerField(verbose_name=_('download limit'), null=True)
time_limit = models.IntegerField(verbose_name=_('expiration delay'), null=True)
category = models.ForeignKey(Category, verbose_name=_('category'), null=False)
nb_document = models.IntegerField(verbose_name=_('number of document'), default=0)
creation_date = models.DateTimeField(auto_now_add=True, verbose_name=_('creation date'), null=False)
modification_date = models.DateTimeField(auto_now=True, verbose_name=_('modification date'), null=False)
class Meta:
verbose_name = _('publication')
verbose_name_plural = _('publication')
def __str__(self):
return f"self.title"
class Document(models.Model):
FORMAT_CHOICES = (
('pdf', 'pdf'),
('epub', 'epub'),
)
age_id = models.CharField(max_length=12, verbose_name=_('publication ID'), unique=True, default='')
language = models.CharField(max_length=2, verbose_name=_('language'), null=False)
format = models.CharField(max_length=10, verbose_name=_('format'), choices=FORMAT_CHOICES, null=False)
title = models.CharField(max_length=512, verbose_name=_('title'), null=False)
publication = models.ForeignKey(Publication, verbose_name=_('publication'), null=False, related_name='documents')
upload = models.FileField(upload_to='media/', validators=[validate_file_extension])
creation_date = models.DateTimeField(auto_now_add=True, verbose_name=_('creation date'), null=False)
modification_date = models.DateTimeField(auto_now=True, verbose_name=_('modification date'), null=False)
class Meta:
verbose_name = _('document')
verbose_name_plural = _('document')
def __str__(self):
return f"self.age_id : self.title - self.publication"
我在表单 python 文件中定义了 formset:
class PublicationForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['category'].empty_label = _('Select a category') # Modify initial empty_label
class Meta:
model = Publication
fields = ['title', 'category']
class DocumentForm(forms.ModelForm):
class Meta:
model = Document
fields = ['publication', 'age_id', 'language', 'format', 'title', 'upload']
DocumentFormSet = inlineformset_factory(Publication, Document, form=DocumentForm, extra=1)
最重要的是,我的视图是在 cruds.py 文件中定义的,如下所示:
class PublicationCreateView(AgeCreateView):
model = Publication
template_name = 'app/publication_form.html'
def get_context_data(self, **kwargs):
context = super(PublicationCreateView, self).get_context_data(**kwargs)
if self.request.POST :
context['document_form'] = DocumentFormSet(self.request.POST, self.request.FILES)
else:
context['document_form'] = DocumentFormSet()
return context
def form_valid(self, form):
context = self.get_context_data()
document = context['document_form']
if document.is_valid():
document.instance = self.object
document.save()
return super(PublicationCreateView, self).form_valid(form)
def get_success_url(self):
return reverse('publication-list-crud')
class PublicationCRUDView(MainConfigCRUDManager):
""" CRUD views for Publication """
model = Publication
default_sort_params = ('category', 'asc')
templates = 'create': 'app/publication_form.html'
custom_views = 'create': PublicationCreateView
#queryset = Publication.objects.annotate(nb_documents=Count('documents'))
# Configuration of fields
search_fields = ['category', 'title']
list_fields = ['category', 'title', 'creation_date', 'modification_date', 'nb_document']
update_fields = ['category', 'title']
class DocumentCRUDView(MainConfigCRUDManager):
""" CRUD views for Document """
model = Document
default_sort_params = ('title', 'asc')
templates = 'create': 'app/publication_form.html'
custom_views = 'create': PublicationCreateView
# Configuration of fields
search_fields = ['age_id', 'title', 'language', 'publication_id.title', 'format']
list_fields = ['age_id', 'title', 'publication', 'language', 'format']
update_fields = ['publication', 'age_id', 'title', 'language', 'format', 'upload']
模板显示很好,有通用的formset,但是当我想提交这个组合表单时,我遇到了这个问题:
异常值:“NoneType”对象没有属性“id”
这是回溯:
追溯:
文件 “/home/.pyenv/versions/Publication3.6.2/lib/python3.6/site-packages/django/core/handlers/exception.py” 在内部 41. response = get_response(request)
文件 “/home/.pyenv/versions/Publication3.6.2/lib/python3.6/site-packages/django/core/handlers/base.py” 在 _get_response 187. response = self.process_exception_by_middleware(e, request)
文件 “/home/.pyenv/versions/Publication3.6.2/lib/python3.6/site-packages/django/core/handlers/base.py” 在 _get_response 185. response = Wrapped_callback(request, *callback_args, **callback_kwargs)
文件 “/home/.pyenv/versions/Publication3.6.2/lib/python3.6/site-packages/django/views/generic/base.py” 在视野中 68. return self.dispatch(request, *args, **kwargs)
文件 “/home/.pyenv/versions/Publication3.6.2/lib/python3.6/site-packages/django/contrib/auth/mixins.py” 在调度中 56. return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)
文件 “/home/.pyenv/versions/Publication3.6.2/lib/python3.6/site-packages/django/contrib/auth/mixins.py” 在调度中 116. return super(UserPassesTestMixin, self).dispatch(request, *args, **kwargs)
文件 “/home/.pyenv/versions/Publication3.6.2/lib/python3.6/site-packages/django/views/generic/base.py” 在调度中 88. return handler(request, *args, **kwargs)
文件 “/home/.pyenv/versions/Publication3.6.2/lib/python3.6/site-packages/django/views/generic/edit.py” 在后 217. return super(BaseCreateView, self).post(request, *args, **kwargs)
文件 “/home/.pyenv/versions/Publication3.6.2/lib/python3.6/site-packages/django/views/generic/edit.py” 在后 183. return self.form_valid(form)
文件 “/home/Bureau/Projets/Publication/publication/src/web/app/cruds.py” 在 form_valid 49. 文档.save()
文件 “/home/.pyenv/versions/Publication3.6.2/lib/python3.6/site-packages/django/forms/models.py” 在保存 666. return self.save_existing_objects(commit) + self.save_new_objects(commit)
文件 “/home/.pyenv/versions/Publication3.6.2/lib/python3.6/site-packages/django/forms/models.py” 在 save_new_objects 800. self.new_objects.append(self.save_new(form, commit=commit))
文件 “/home/.pyenv/versions/Publication3.6.2/lib/python3.6/site-packages/django/forms/models.py” 在 save_new 中 946. pk_value = getattr(self.instance, self.fk.remote_field.field_name)
异常类型:AttributeError at /crud/publication/create/ 异常 值:“NoneType”对象没有属性“id”
我不太了解id
的定义位置以及如何解决此问题。
谢谢
【问题讨论】:
【参考方案1】:我找到了答案,但我不知道为什么它现在有效,而不是以前。
之前的form_valid()
函数:
def form_valid(self, form):
context = self.get_context_data()
document = context['document_form']
if document.is_valid():
document.instance = self.object
document.save()
return super(PublicationCreateView, self).form_valid(form)
这是我的新 form_valid()
函数:
def form_valid(self, form):
context = self.get_context_data()
document = context['document_form']
if document.is_valid():
self.object = form.save() #I added this line
document.instance = self.object
document.save()
return super(PublicationCreateView, self).form_valid(form)
【讨论】:
你明白异常告诉你什么吗?代码试图访问类型为 NoneType 的变量的字段“id”,这意味着该值为None
(== null,未定义)。您通常通过确保您的相关变量实际上包含值而不是 None
... 来解决这个问题,这是您通过分配 document.instance
在答案中所做的。
好的,非常感谢您的解释!我必须更深入地了解.instance
,以便了解在哪种情况下我必须这样做,或者我可以使用它。以上是关于Django CBV - Formsets:'NoneType'对象没有属性'id'的主要内容,如果未能解决你的问题,请参考以下文章
Tutorial : Implementing Django Formsets
Django Crispy Forms、Twitter Bootstrap 和 Formsets with form-inline
Django报错:AttributeError: 'function' object has no attribute 'as_view'