我是Django的新手,正在努力让嵌套路由器工作。基本上,我试图对端点/api/v1/organizers/1/events/1
建模,其中我有Event
和Organizer
模型,如下所示:
class Event(models.Model):
class Meta:
db_table = 'events'
STATUS_CHOICES = (
('scheduled', 'Scheduled'),
('completed', 'Completed'),
('cancelled', 'Cancelled')
)
name = models.TextField()
purpose = models.TextField()
date_time = models.DateTimeField()
description = models.TextField()
programme = models.TextField()
entry_fee = models.DecimalField(max_digits=6, decimal_places=2)
status = models.TextField(choices=STATUS_CHOICES)
comments = models.TextField(null=True)
people_invited = models.ManyToManyField('Contact', through='EventInvitation')
organizer = models.ForeignKey('Organizer', on_delete=models.CASCADE)
class Organizer(models.Model):
STATUS_CHOICES = (
('inactive', 'Inactive'),
('active', 'Active'),
)
class Meta:
db_table = 'organizers'
first_name = models.TextField()
middle_name = models.TextField(null=True)
last_name = models.TextField(null=True)
email = models.OneToOneField('Email', on_delete=models.CASCADE)
company_name = models.TextField(null=True)
company_description = models.TextField(null=True)
password = models.TextField()
login_token = models.TextField(null=True)
registered_on = models.DateTimeField(null=True)
status = models.TextField(choices = STATUS_CHOICES, default='inactive')
字符串
我创建了另一个名为rest_api
的应用程序来处理API。模型存储在名为shared_stuff
的应用程序中。无论如何,这是项目级的urls.py
(现在不要介意front_end
应用程序):
from django.conf.urls import include, url
urlpatterns = [
url(r'^api/v1/', include('rest_api.urls')),
url(r'^', include('frontend.urls')),
]
型
以下是来自rest_api
应用程序的urls.py
:
from django.conf.urls import url, include
from rest_framework_nested import routers
from .views import *
router = routers.SimpleRouter()
# /organizers/12/events/1
router.register(r'organizers', OrganizerViewSet, base_name='organizers')
organizer_router = routers.NestedSimpleRouter(router, r'organizers', lookup='organizers')
organizer_router.register(r'events', EventViewSet, base_name='organizers-events')
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^', include(organizer_router.urls)),
]
型
以下是rest_api
的serializers.py
应用程序:
class EventSerializer(serializers.ModelSerializer):
class Meta:
model = Event
fields = ['id', 'name', 'purpose', 'date_time', 'description', 'programme', 'entry_fee', 'status', 'comments']
class OrganizerSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Organizer
fields = ['id', 'first_name', 'middle_name', 'last_name', 'email', 'company_name', 'company_description', 'registered_on', 'status']
events = serializers.HyperlinkedIdentityField(
view_name = 'events_list',
lookup_url_kwarg = 'organizer_pk'
)
型
最后,这是来自应用程序rest_api
的views.py
:
from rest_framework import viewsets
from .models import *
from .serializers import *
class EventViewSet(viewsets.ModelViewSet):
def list(self, request, organizer_pk=None, name='events_list'):
events = self.queryset.filter(organizer=organizer_pk)
serializer = EventSerializer(events, many=True)
return Response(serializer.data)
class OrganizerViewSet(viewsets.ModelViewSet):
def list(self, request, name='organizers_list'):
data = Organizer.objects.all()
serializer = OrganizerSerializer(data, many=True)
return Response(serializer.data)
型
我确信在我的代码中有很多东西被破坏了,这就是我需要帮助的地方。问题是我得到了以下错误:TypeError: list() got an unexpected keyword argument 'organizers_pk'
个
我不知道什么是错的,并将感谢一些帮助!
2条答案
按热度按时间evrscar21#
我通过将
EventViewSet
更改为以下内容来实现它:字符串
我不知道为什么,但预期的关键字参数名称是
organizers_pk
,而我有organizer_pk
。我想知道为什么会这样,但除此之外,我的问题现在解决了。ejk8hzay2#
有点过时的答案,但你有:
第一个月
无论你在
lookup
中输入什么,最终都会成为父资源pk的前缀。在这种情况下,将你的URL更改为:organizer_router = routers.NestedSimpleRouter(router, r'organizers', lookup='organizer')
个会帮你完成任务