[英]Django Rest Framework: How to implement a nested logic?
Let's say I have three models as:假设我有三个模型:
class User(AppModel):
name = models.CharField(max_length=255)
class Business(AppModel):
owner = models.ForeignKey("User", related_name="businesses", on_delete=models.CASCADE)
legal_name = models.CharField(max_length=255)
class Invoice(AppModel):
business = models.ForeignKey("Business", related_name="invoices", on_delete=models.CASCADE)
amount = models.integerField()
As you can see, a user
can have multiple businesses
and a business
can have multiple invoices
.如您所见,一个user
可以有多个businesses
,一个business
可以有多个invoices
。
My serializers.py:我的序列化程序.py:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields= ('name')
class BusinessSerializer(serializers.ModelSerializer):
owner = UserSerializer(many=False)
class Meta:
model = Business
fields= ('owner','legal_name')
class InvoiceSerializer(serializers.ModelSerializer):
business= BusinessSerializer(many=False)
class Meta:
model = Invoice
fields= ('business','amount')
views.py:视图.py:
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
class BusinessViewSet(viewsets.ModelViewSet):
queryset = Business.objects.all()
serializer_class = BusinessSerializer
class InvoiceViewSet(viewsets.ModelViewSet):
queryset = Invoice.objects.all()
serializer_class = InvoiceSerializer
urls.py:网址.py:
router = DefaultRouter()
router.register('user', UserViewSet, base_name='users')
router.register('business', BusinessViewSet, base_name='businesses')
router.register('invoice', InvoiceViewSet, base_name='invoices')
urlpatterns = router.urls
http://example.com/api/user
returns all users. http://example.com/api/user
返回所有用户。 Not a problem.不是问题。
But the functionality I'm looking for is:但我正在寻找的功能是:
http://example.com/api/business/
returns http://example.com/api/business/
返回
[ { "legal_name": "1business", "owner": 1, }, { "legal_name": "2business", "owner": 1, },]
http://example.com/api/business/1/
returns http://example.com/api/business/1/
返回
{ "legal_name": "1business", "owner": 1, }
The above is ok.以上是可以的。 But I also need:但我还需要:
http://example.com/api/business/1/invoices/
should return http://example.com/api/business/1/invoices/
应该返回
[ { "business": 1, "amount": 100, }, { "business": 1, "amount": 999, },]
As well I should be able to create update delete those invoices there.同样,我应该能够在那里创建更新删除那些发票。
Any Help?任何帮助? I'm new to django rest framework.我是 Django 休息框架的新手。 The above classes are just a sample.上面的类只是一个示例。 Ignore errors.忽略错误。
Add additional viewset action
in your BusinessViewSet
as 在您的BusinessViewSet
添加其他viewset action
为
class BusinessViewSet(viewsets.ModelViewSet):
queryset = Business.objects.all()
serializer_class = BusinessSerializer
def get_invoice(self, request, *args, **kwargs): invoice_queryset = self.get_object().invoices.all() serializer = InvoiceSerializer(invoice_queryset, many=True) return Response(serializer.data)
and change your urls.py
as, 并将您的urls.py
更改为
urlpatterns = [
url(r'business/(?P<pk>\d+)/invoice/', BusinessViewSet.as_view({"get": "get_invoice"})),
] + router.urls
You should use django decorators which are @list_route
and @detail_route
for your viewset. 你应该使用Django装饰这是@list_route
和@detail_route
您的视图集中。 But be careful with your DRF version. 但是请注意您的DRF版本。 Because those decorators merged together as @action
in DRF 3.8+. 因为这些装饰器在DRF 3.8+中作为@action
合并在一起。 Here is the announcement . 这是公告 。
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework import status
class BusinessViewSet(viewsets.ModelViewSet):
queryset = Business.objects.all()
serializer_class = BusinessSerializer
@action(detail=True, methods=["GET"], url_path="invoices")
def invoices(self, request, pk=None):
"""
Your codes comes here to return related result.
pk variable contains the param value from url.
if you do not specify the url_path properties then action will accept the function's name as url path.
"""
entity = Invoice.objects.filter(business=pk)
serializer = self.get_serializer(entity, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
Then, you will be able to call this endpoints from; 然后,您将可以从以下位置调用此端点:
http://example.com/api/business/{{PK}}/invoices/
http://example.com/api/business/1/invoices/
http://example.com/api/business/3/invoices/
http://example.com/api/business/23/invoices/
Here you can find more details about @actions
from documentation . 在这里,您可以从文档中找到有关@actions
更多详细信息。
PS: Don't forget to control empty entity results in your codes. PS:不要忘记在代码中控制空实体结果。 You should return correct response with correct status codes. 您应该使用正确的状态代码返回正确的响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.