我想从视图中传递来自网址的数据。 我读了this并试着写! 但是我收到了一个错误:
__init__() takes 1 positional argument but 2 were given
serializer.py:
class ContactsDetailsSerializer(serializers.ModelSerializer):
class Meta:
model = Contacts
fields = ('id', 'profile_id', 'tel','email')
views.py:
class ContactsDetailsViewSet(viewsets.ViewSet):
def list(self, request ):
queryset = Contacts.objects.all()
serializer = ContactsSerializer(queryset, many=True)
return Response(serializer.data)
urls.py:
router = routers.DefaultRouter()
router.register(r'profiles', views.ProfileViewSet)
# router.register(r'users', views.UserViewSet)
router.register(r'members', views.MemberViewSet)
router.register(r'files', views.FilesViewSet)
router.register(r'contacts', views.ContactsViewSet)
router.register(r'products', views.ProductsViewSet)
router.register(r'stories', views.StoriesViewSet)
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('allauth.urls')),
url(r'^', include(router.urls)),
url(r'^test', ContactsDetailsViewSet.as_view({'get': 'list'})),//here
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]
任何人都可以提供帮助吗?如何传递数据(通过REST框架)?
答案 0 :(得分:0)
{Name:table1 Rows:[{Cols:[{Name:col1 Value:value1} {Name:col2 Value:value2} {Name:col3 Value:value3}]} {Cols:[{Name:col1 Value:value4} {Name:col2 Value:value5} {Name:col3 Value:value6}]}]}
{Name:table2 Rows:[{Cols:[{Name:col1 Value:value7} {Name:col2 Value:value8} {Name:col3 Value:value9} {Name:col4 Value:valueA}]}]}
EOF
您需要使用映射调用url('^test/(?P<profile_id>.+)$', views.ContactsDetailsViewSet),
。这不是常规视图,而是ViewSet:
as_view()
另请注意,您需要将url('^test/(?P<profile_id>.+)$', views.ContactsDetailsViewSet).as_view({'get': 'retrieve'})),
指定为profile_id
。