获取1-4个项目通过Django Rest Framework

huangapple go评论71阅读模式
英文:

Get 1-4 Items via Django Rest Framework

问题

在我的Web应用中,用户可以选择比较2、3或4个数据库(Postgres)中的项目。我已经能够创建一个视图函数来查询固定数量的项目。

例如,查询3个项目

视图函数

from myapp.models import Item

@api_view(['GET'])
def get_three_items(request, pk1, pk2, pk3):
    item1 = Item.objects.get(pk=pk1)
    item2 = Item.objects.get(pk=pk2)
    item3 = Item.objects.get(pk=pk3)
    data = [item1.serialize(), item2.serialize(), item3.serialize()]
    return Response(data)

urls.py

from django.urls import path
from .views import get_three_items

urlpatterns = [
    path('items/<int:pk1>/<int:pk2>/<int:pk3>/', get_three_items),
    # ...
]

但是在我的Web应用中,用户可以选择比较2、3或4个项目。是否有办法以动态方式编写视图函数和URL,或者我必须为每种比较能力创建一个视图?因此,一个用于比较2个项目的视图,一个用于比较3个项目的视图,以及一个用于比较4个项目的视图?

英文:

I want to create awebapp where the user can choose between comparing 2,3 or 4 items of the database (Postgres).

i already could create a view function to query a fixed amount of items.

e.g query 3 Items

View Function

from myapp.models import Item

@api_view([&#39;GET&#39;])
def get_three_items(request, pk1, pk2, pk3):
    item1 = Item.objects.get(pk=pk1)
    item2 = Item.objects.get(pk=pk2)
    item3 = Item.objects.get(pk=pk3)
    data = [item1.serialize(), item2.serialize(), item3.serialize()]
    return Response(data)

urls.py

from django.urls import path
from .views import get_three_items

urlpatterns = [
    path(&#39;items/&lt;int:pk1&gt;/&lt;int:pk2&gt;/&lt;int:pk3&gt;/&#39;, get_three_items),
    # ...
]

But in my web app the user is able to choose between comparing 2,3 or 4 items.
Is there a way to write the View Function and URL in a way, that it is dynamic or do i have to create a view for each compare capacity. So a view to compare 2 items. A view to compare 3 items and a view to compare 4 items?

答案1

得分: 1

如果在查询参数中传递ID是一种选项,那么你可以像这样操作:

views.py

from rest_framework.decorators import api_view
from rest_framework.response import Response

from .models import Item

@api_view(['GET'])
def compare_items(request):
    items_ids = request.query_params.getlist('items_ids')
    items = Item.objects.filter(pk__in=items_ids)
    data = [item.serialize() for item in items]
    return Response(data)

urls.py

from django.urls import path
from .views import compare_items

urlpatterns = [
    path('items/', compare_items),
]

然后你可以像这样传递ID:

HOST/items/?items_ids=1&items_ids=3
英文:

If passing ids in Query Params is an option, then you could do something like that:

views.py

from rest_framework.decorators import api_view
from rest_framework.response import Response

from .models import Item


@api_view([&#39;GET&#39;])
def compare_items(request):
    items_ids = request.query_params.getlist(&#39;items_ids&#39;)
    items = Item.objects.filter(pk__in=items_ids)
    data = [item.serialize() for item in items]
    return Response(data)

urls.py

from django.urls import path
from .views import compare_items

urlpatterns = [
    path(&#39;items/&#39;, compare_items),
]

and then you can pass ids like that:

HOST/items/?items_ids=1&amp;items_ids=3

答案2

得分: 1

I think i found the solution:

我认为我找到了解决方案:

I adjusted PTomasz code a little. Try this version. It worked in my case.

我稍微调整了PTomasz的代码。尝试这个版本。这在我的情况下有效。

from rest_framework.decorators import api_view
from rest_framework.response import Response

from .models import Item


@api_view(['GET'])
def compare_items(request):
    items_ids = request.query_params.getlist('items_ids')
    items = Item.objects.filter(pk__in=items_ids)
    data = [serializer(item, many=False).data for item in items]
    return Response(data)
英文:

I think i found the solution:

i adjusted PTomasz code a little. Try this version. It worked in my case.

from rest_framework.decorators import api_view
from rest_framework.response import Response

from .models import Item


@api_view([&#39;GET&#39;])
def compare_items(request):
    items_ids = request.query_params.getlist(&#39;items_ids&#39;)
    items = Item.objects.filter(pk__in=items_ids)
    data = [serializer(item, many=False).data for item in items]
    return Response(data)

huangapple
  • 本文由 发表于 2023年3月15日 20:15:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定