2017-08-03 157 views
1

下午好。嘗試創建SubscriptionList模型的對象時。我有這個錯誤。我對這個話題做了一個研究,但還沒有找到相關的答案。類型對象'SubscriptionList'沒有屬性'_meta'

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) 

的url

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

回答

1

你有一個視圖SubscriptionList這是與您的模型SubscriptionList相沖突。您可以通過重命名視圖來解決問題(並更新相關的URL模式。

+0

感謝您抽出寶貴時間。 – madjack99

相關問題