我正在使用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文件中定义了表单集:
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']
模板显示良好,具有常见的表单集,但是当我想提交此组合表单时,会遇到此问题:
异常值:'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 = wrapd_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(请求,* 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. document.save()
文件 “ /home/.pyenv/versions/Publication3.6.2/lib/python3.6/site-packages/django/forms/models.py” 在保存 666.返回self.save_existing_objects(提交)+ self.save_new_objects(提交)
文件 “ /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)
异常类型:/ crud / publication / create / Exception中的AttributeError 值:'NoneType'对象没有属性'id'
我对id
的定义以及如何解决此问题不太了解。
谢谢
答案 0 :(得分:0)
我找到了答案,但是我不知道为什么现在可以解决这个问题。
上一个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)