Django Rest Framework并不适用于一对一的关系

时间:2018-05-09 14:34:05

标签: python django django-rest-framework

我有几个视图集完美运行,但那些OneToOneField的视图集只会出错。这很奇怪,因为我只是复制粘贴并多次更改模型引用,只有具有一对一字段的那些引起同样的错误:

这是模型:

class Provider(models.Model):
    company = models.OneToOneField('Company', on_delete=models.CASCADE, primary_key=True)
    provider_type = models.ForeignKey('ProviderType', on_delete=models.PROTECT, related_name='type_providers')
    is_active = models.BooleanField(default=True)

这是序列化器:

class ProviderSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = models.Provider
        fields = ('company', 'provider_type', 'is_active')

这是视图集:

class ProviderViewSet(viewsets.ModelViewSet):
    queryset = models.Provider.objects.all()
    serializer_class = serializers.ProviderSerializer

我的urls.py

router = routers.DefaultRouter()
router.register(r'countries', views.CountryViewSet)
router.register(r'states', views.StateViewSet)
router.register(r'cities', views.CityViewSet)
router.register(r'zones', views.ZoneViewSet)
router.register(r'languages', views.LanguageViewSet)
router.register(r'agency_groups', views.AgencyGroupViewSet)
router.register(r'provider_types', views.ProviderTypeViewSet)
router.register(r'room_types', views.RoomTypeViewSet)
router.register(r'plan_types', views.PlanTypeViewSet)
router.register(r'hotel_categories', views.HotelCategoryViewSet)
router.register(r'hotels', views.HotelViewSet)
router.register(r'providers', views.ProviderViewSet)
router.register(r'agencies', views.AgencyViewSet)
router.register(r'company_hotels', views.CompanyHotelViewSet)
router.register(r'company_agencies', views.CompanyAgencyViewSet)
router.register(r'company_provider', views.CompanyProviderViewSet)
router.register(r'companies', views.CompanyViewSet)
router.register(r'contacts', views.ContactViewSet)

countries_list = views.CountryViewSet.as_view({
    'get':'list',
    'post':'create'
})

country_detail = views.CountryViewSet.as_view({
    'get':'retrieve',
    'put':'update',
    'patch':'partial_update',
    'delete':'destroy'
})

states_list = views.StateViewSet.as_view({
    'get':'list',
    'post':'create'
})

state_detail = views.StateViewSet.as_view({
    'get':'retrieve',
    'put':'update',
    'patch':'partial_update',
    'delete':'destroy'
})

cities_list = views.CityViewSet.as_view({
    'get':'list',
    'post':'create'
})

city_detail = views.CityViewSet.as_view({
    'get':'retrieve',
    'put':'update',
    'patch':'partial_update',
    'delete':'destroy'
})

zones_list = views.ZoneViewSet.as_view({
    'get':'list',
    'post':'create'
})

zone_detail = views.ZoneViewSet.as_view({
    'get':'retrieve',
    'put':'update',
    'patch':'partial_update',
    'delete':'destroy'
})

languages_list = views.LanguageViewSet.as_view({
    'get':'list',
    'post':'create'
})

language_detail = views.LanguageViewSet.as_view({
    'get':'retrieve',
    'put':'update',
    'patch':'partial_update',
    'delete':'destroy'
})

agency_groups_list = views.AgencyGroupViewSet.as_view({
    'get':'list',
    'post':'create'
})

agency_group_detail = views.AgencyGroupViewSet.as_view({
    'get':'retrieve',
    'put':'update',
    'patch':'partial_update',
    'delete':'destroy'
})

provider_types_list = views.ProviderTypeViewSet.as_view({
    'get':'list',
    'post':'create'
})

provider_type_detail = views.ProviderTypeViewSet.as_view({
    'get':'retrieve',
    'put':'update',
    'patch':'partial_update',
    'delete':'destroy'
})

room_types_list = views.RoomTypeViewSet.as_view({
    'get':'list',
    'post':'create'
})

room_type_detail = views.RoomTypeViewSet.as_view({
    'get':'retrieve',
    'put':'update',
    'patch':'partial_update',
    'delete':'destroy'
})

plan_types_list = views.PlanTypeViewSet.as_view({
    'get':'list',
    'post':'create'
})

plan_type_detail = views.PlanTypeViewSet.as_view({
    'get':'retrieve',
    'put':'update',
    'patch':'partial_update',
    'delete':'destroy'
})

hotel_categories_list = views.HotelCategoryViewSet.as_view({
    'get':'list',
    'post':'create'
})

hotel_category_detail = views.HotelCategoryViewSet.as_view({
    'get':'retrieve',
    'put':'update',
    'patch':'partial_update',
    'delete':'destroy'
})

hotels_list = views.HotelViewSet.as_view({
    'get':'list',
    'post':'create'
})

hotel_detail = views.HotelViewSet.as_view({
    'get':'retrieve',
    'put':'update',
    'patch':'partial_update',
    'delete':'destroy'
})

company_hotels_list = views.CompanyHotelViewSet.as_view({
    'get':'list',
    'post':'create'
})

company_hotel_detail = views.CompanyHotelViewSet.as_view({
    'get':'retrieve',
    'put':'update',
    'patch':'partial_update',
    'delete':'destroy'
})

company_agencies_list = views.CompanyAgencyViewSet.as_view({
    'get':'list',
    'post':'create'
})

company_agency_detail = views.CompanyAgencyViewSet.as_view({
    'get':'retrieve',
    'put':'update',
    'patch':'partial_update',
    'delete':'destroy'
})

company_providers_list = views.CompanyProviderViewSet.as_view({
    'get':'list',
    'post':'create'
})

company_provider_detail = views.CompanyProviderViewSet.as_view({
    'get':'retrieve',
    'put':'update',
    'patch':'partial_update',
    'delete':'destroy'
})

companies_list = views.CompanyViewSet.as_view({
    'get':'list',
    'post':'create'
})

company_detail = views.CompanyViewSet.as_view({
    'get':'retrieve',
    'put':'update',
    'patch':'partial_update',
    'delete':'destroy'
})

providers_list = views.ProviderViewSet.as_view({
    'get':'list',
    'post':'create'
})

provider_detail = views.ProviderViewSet.as_view({
    'get':'retrieve',
    'put':'update',
    'patch':'partial_update',
    'delete':'destroy'
})

agencies_list = views.AgencyViewSet.as_view({
    'get':'list',
    'post':'create'
})

agency_detail = views.AgencyViewSet.as_view({
    'get':'retrieve',
    'put':'update',
    'patch':'partial_update',
    'delete':'destroy'
})

contacts_list = views.ContactViewSet.as_view({
    'get':'list',
    'post':'create'
})

contact_detail = views.ContactViewSet.as_view({
    'get':'retrieve',
    'put':'update',
    'patch':'partial_update',
    'delete':'destroy'
})

urlpatterns = [
    path('', include(router.urls)),
    path('countries/', countries_list, name='rest_countries_list'),
    path('countries/<int:pk>/', country_detail, name='rest_country_detail'),
    path('countries/<int:pk>/states/', states_list, name='rest_states_list'),
    path('states/<int:pk>/', state_detail, name='rest_state_detail'),
    path('states/<int:pk>/cities/', cities_list, name='rest_cities_list'),
    path('cities/<int:pk>/', city_detail, name='rest_city_detail'),
    path('cities/<int:pk>/zones', zones_list, name='rest_zones_list'),
    path('zones/<int:pk>/', zone_detail, name='rest_zone_detail'),
    path('languages/', languages_list, name='rest_languages_list'),
    path('languages/<int:pk>/', language_detail, name='rest_language_detail'),
    path('agency_groups/', agency_groups_list, name='rest_agency_groups_list'),
    path('agency_groups/<int:pk>/', agency_group_detail, name='rest_agency_group_detail'),
    path('provider_types/', provider_types_list, name='rest_provider_types_list'),
    path('provider_types/<int:pk>/', provider_type_detail, name='rest_provider_type_detail'),
    path('room_types/', room_types_list, name='rest_room_types_list'),
    path('room_types/<int:pk>/', room_type_detail, name='rest_room_type_detail'),
    path('plan_types/', plan_types_list, name='rest_plan_types_list'),
    path('plan_types/<int:pk>/', plan_type_detail, name='rest_plan_type_detail'),
    path('hotel_categories/', hotel_categories_list, name='rest_hotel_categories_list'),
    path('hotel_categories/<int:pk>/', hotel_category_detail, name='rest_hotel_category_detail'),
    path('companies/', companies_list, name='rest_companies_list'),
    path('companies/<int:pk>/', company_detail, name='rest_company_detail'),
    path('companies/<int:pk>/hotel', hotel_detail, name='rest_hotel_detail'),
    path('companies/<int:pk>/provider', provider_detail, name='rest_provider_detail'),
    path('companies/<int:pk>/agency', agency_detail, name='rest_agencie_detail'),
    path('hotels/', hotels_list, name='rest_hotels_list'),
    path('hotels/<int:pk>/', hotel_detail, name='rest_hotel_detail'),
    path('providers/', providers_list, name='rest_providers_list'),
    path('providers/<int:pk>/', provider_detail, name='rest_provider_detail'),
    path('agencies/', agencies_list, name='rest_agencies_list'),
    path('agencies/<int:pk>/', agency_detail, name='rest_agencies_detail'),
    path('company_hotels/', company_hotel_detail, name='rest_company_hotel_detail'),
    path('company_hotels/<int:pk>/', company_hotel_detail, name='rest_company_hotel_detail'),
    path('company_agencies/', company_agencies_list, name='rest_company_agencies_list'),
    path('company_agencies/<int:pk>/', company_agency_detail, name='rest_company_agency_detail'),
    path('company_providers/', company_providers_list, name='rest_company_providers_list'),
    path('company_providers/<int:pk>/', company_provider_detail, name='rest_company_provider_detail'),
    path('contacts/', contacts_list, name='rest_contacts_list'),
    path('contacts/<int:pk>/', contact_detail, name='rest_contact_detail'),
]

我的所有模型对象都具有完全相同的结构,并且它适用于所有模型对象,但适用于具有一对一字段的人。即使对于具有外键(一对多关系)的模型,它也能正常工作。

这是整个追溯:

Internal Server Error: /general/hotels/
Traceback (most recent call last):
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/viewsets.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/views.py", line 483, in dispatch
    response = self.handle_exception(exc)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/views.py", line 443, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/views.py", line 480, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/mixins.py", line 48, in list
    return Response(serializer.data)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/serializers.py", line 765, in data
    ret = super(ListSerializer, self).data
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/serializers.py", line 262, in data
    self._data = self.to_representation(self.instance)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/serializers.py", line 683, in to_representation
    self.child.to_representation(item) for item in iterable
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/serializers.py", line 683, in <listcomp>
    self.child.to_representation(item) for item in iterable
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/serializers.py", line 510, in to_representation
    fields = self._readable_fields
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/serializers.py", line 376, in _readable_fields
    field for field in self.fields.values()
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/serializers.py", line 363, in fields
    for key, value in self.get_fields().items():
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/serializers.py", line 1054, in get_fields
    fields[field_name] = field_class(**field_kwargs)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/relations.py", line 275, in __init__
    assert self.view_name is not None, 'The `view_name` argument is required.'
AssertionError: The `view_name` argument is required.
[09/May/2018 15:23:24] "GET /general/hotels/ HTTP/1.1" 500 132830

0 个答案:

没有答案