下午好。在尝试创建SubscriptionList模型的对象时。我有这个错误。我对这个话题进行了研究,但没有找到相对的答案。
Traceback:
File "D:\PyDocs\taskagain\lib\site-packages\django\core\handlers\exception.py" in inner
41. response = get_response(request)
File "D:\PyDocs\taskagain\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "D:\PyDocs\taskagain\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\PyDocs\taskagain\lib\site-packages\django\views\generic\base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "D:\PyDocs\taskagain\lib\site-packages\django\views\generic\base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "D:\PyDocs\taskagain\lib\site-packages\django\views\generic\edit.py" in get
213. return super(BaseCreateView, self).get(request, *args, **kwargs)
File "D:\PyDocs\taskagain\lib\site-packages\django\views\generic\edit.py" in get
174. return self.render_to_response(self.get_context_data())
File "D:\PyDocs\taskagain\lib\site-packages\django\views\generic\edit.py" in get_context_data
93. kwargs['form'] = self.get_form()
File "D:\PyDocs\taskagain\lib\site-packages\django\views\generic\edit.py" in get_form
44. form_class = self.get_form_class()
File "D:\PyDocs\taskagain\lib\site-packages\django\views\generic\edit.py" in get_form_class
132. return model_forms.modelform_factory(model, fields=self.fields)
File "D:\PyDocs\taskagain\lib\site-packages\django\forms\models.py" in modelform_factory
558. return type(form)(class_name, (form,), form_class_attrs)
File "D:\PyDocs\taskagain\lib\site-packages\django\forms\models.py" in __new__
261. apply_limit_choices_to=False,
File "D:\PyDocs\taskagain\lib\site-packages\django\forms\models.py" in fields_for_model
144. opts = model._meta
Exception Type: AttributeError at /blogs/1/add_to_subscription/
Exception Value: type object 'SubscriptionList' has no attribute '_meta'
模型
class SubscriptionList(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE,
related_name='subscription_list')
blogs_in_subscription = models.ManyToManyField(Blog, related_name='in_subscription_lists',
blank=True)
def __str__(self):
return "%s subscription list" % self.user.username
浏览
class AddBlogToSubList(CreateView):
model = SubscriptionList
fields = ['user', 'blogs_in_subscription']
template_name = 'blog/add_blog_to_sub_list.html'
success_url = reverse_lazy('blog:blog-list')
def form_valid(self, form):
form.instance.blogs_in_subscription = Blog.objects.get(pk=self.kwargs['blog_pk'])
form.instance.user = self.request.user
return super(AddBlogToSubList, self).form_valid(form)
网址
urlpatterns = [
url(r'^$', BlogList.as_view(), name='blog-list'),
url(r'^(?P<blog_pk>\d+)/$', BlogDetail.as_view(), name='blog-detail'),
# url(r'^(?P<blog_pk>\d+)/add_to_subscription/$', AddBlogToSubList.as_view(), name='add-to-subscription'),
url(r'^(?P<blog_pk>\d+)/subscriptions/$', SubscriptionList.as_view(), name='subscription-list'),
url(r'^(?P<blog_pk>\d+)/create/$', PostCreate.as_view(), name='post-create'),
url(r'^(?P<blog_pk>\d+)/(?P<post_pk>\d+)/$', PostDetail.as_view(), name='post-detail'),
url(r'^(?P<blog_pk>\d+)/(?P<post_pk>\d+)/update/$', PostUpdate.as_view(), name='post-update'),
url(r'^(?P<blog_pk>\d+)/(?P<post_pk>\d+)/delete/$', PostDelete.as_view(), name='post-delete'),
如果有人能够提供帮助,我将不胜感激。 python 3.6 / django1.11
答案 0 :(得分:1)
您的视图SubscriptionList
与您的模型SubscriptionList
发生冲突。您可以通过重命名视图来修复问题(并更新相关的URL模式。