Django RESTful API 错误 "type object 'User' has no attribute '_meta'"
Django RESTful API error "type object 'User' has no attribute '_meta'"
我正在尝试使用 django-rest-framework 和 cassandra 构建 RESTful API,逐步遵循 Django-rest-framework 上的官方 django-rest-framework 教程我遇到了这个错误
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in dispatch
456. response = self.handle_exception(exc)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in dispatch
444. self.initial(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in initial
359. self.check_permissions(request)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in check_permissions
304. if not permission.has_permission(request, self):
File "/usr/local/lib/python2.7/dist-packages/rest_framework/permissions.py" in has_permission
125. perms = self.get_required_permissions(request.method, queryset.model)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/permissions.py" in get_required_permissions
104. 'app_label': model_cls._meta.app_label,
Exception Type: AttributeError at /users/
Exception Value: type object 'User' has no attribute '_meta'
这是我的代码
urls.py
from django.conf.urls import url
from rest_framework.urlpatterns import format_suffix_patterns
from tutorial import views
urlpatterns = [
# Examples:
url(r'^users/$', views.UsersList.as_view()),
url(r'^users/(?P<pk>[0-9]+)/$', views.UserDetail.as_view()),
]
urlpatterns = format_suffix_patterns(urlpatterns)
models.py
import uuid
from cassandra.cqlengine import columns
from cassandra.cqlengine.models import Model
# Create your models here.
class User(Model):
id = columns.UUID(primary_key=True, default=uuid.uuid4)
username = columns.Text(max_length=30, required=True)
password = columns.Text(max_length=55, required=True)
gender = columns.UUID(default=0)
languages = columns.List(value_type=columns.UUID)
friends = columns.Set(value_type=columns.UUID)
serializers.py
from rest_framework import serializers
from tutorial.models import User
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('username', 'position', 'gender', 'status', 'language')
views.py
from tutorial.models import User
from tutorial.serializers import UserSerializer
from rest_framework import generics
class UsersList(generics.ListCreateAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
class UserDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
提前感谢您的支持。
据我所知,cassandra 的模型没有实现元 class,这是与 rest_framework 这样的应用程序一起工作所必需的。您将需要实现 django 兼容模型才能使用 rest_framework.
正如 django-cassandra-engine 的一位开发人员所述:
cqlengine is about to be merged into python-driver soon. After that we can think about it, but it is important to know, that django-cassandra-engine never meant to be fully compatible with django models.
Github 问题 here
引用这个项目
https://github.com/jbatalle/django-cassandra-engine/tree/master/testproject
它使用 serializers.Serializer 而不是 serializers.ModelSerializer
我正在尝试使用 django-rest-framework 和 cassandra 构建 RESTful API,逐步遵循 Django-rest-framework 上的官方 django-rest-framework 教程我遇到了这个错误
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in dispatch
456. response = self.handle_exception(exc)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in dispatch
444. self.initial(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in initial
359. self.check_permissions(request)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in check_permissions
304. if not permission.has_permission(request, self):
File "/usr/local/lib/python2.7/dist-packages/rest_framework/permissions.py" in has_permission
125. perms = self.get_required_permissions(request.method, queryset.model)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/permissions.py" in get_required_permissions
104. 'app_label': model_cls._meta.app_label,
Exception Type: AttributeError at /users/
Exception Value: type object 'User' has no attribute '_meta'
这是我的代码
urls.py
from django.conf.urls import url
from rest_framework.urlpatterns import format_suffix_patterns
from tutorial import views
urlpatterns = [
# Examples:
url(r'^users/$', views.UsersList.as_view()),
url(r'^users/(?P<pk>[0-9]+)/$', views.UserDetail.as_view()),
]
urlpatterns = format_suffix_patterns(urlpatterns)
models.py
import uuid
from cassandra.cqlengine import columns
from cassandra.cqlengine.models import Model
# Create your models here.
class User(Model):
id = columns.UUID(primary_key=True, default=uuid.uuid4)
username = columns.Text(max_length=30, required=True)
password = columns.Text(max_length=55, required=True)
gender = columns.UUID(default=0)
languages = columns.List(value_type=columns.UUID)
friends = columns.Set(value_type=columns.UUID)
serializers.py
from rest_framework import serializers
from tutorial.models import User
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('username', 'position', 'gender', 'status', 'language')
views.py
from tutorial.models import User
from tutorial.serializers import UserSerializer
from rest_framework import generics
class UsersList(generics.ListCreateAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
class UserDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
提前感谢您的支持。
据我所知,cassandra 的模型没有实现元 class,这是与 rest_framework 这样的应用程序一起工作所必需的。您将需要实现 django 兼容模型才能使用 rest_framework.
正如 django-cassandra-engine 的一位开发人员所述:
cqlengine is about to be merged into python-driver soon. After that we can think about it, but it is important to know, that django-cassandra-engine never meant to be fully compatible with django models.
Github 问题 here
引用这个项目 https://github.com/jbatalle/django-cassandra-engine/tree/master/testproject
它使用 serializers.Serializer 而不是 serializers.ModelSerializer