[英]I have to pass the multiple <int:pk> in a single endpoint for django rest api?
Here in the above picture, I have all the packages shown for single destination id ie pk=1.在上图中,我显示了单个目标 id 的所有包,即 pk=1。 Now, what i want to show is single package of single destination ie my endpoint should be localhost/api/destination/1/package/1 or something like this.现在,我想展示的是单个目的地的单个包,即我的端点应该是 localhost/api/destination/1/package/1 或类似的东西。 My views and serializers are as shown below:我的视图和序列化程序如下所示:
class Destination(models.Model):
name = models.CharField(max_length=255, unique=True)
dest_image = models.ImageField(blank=True)
thumbnail = ImageSpecField(source='dest_image',
processors=[ResizeToFill(100, 50)],
format='JPEG',
options={'quality': 60})
def __str__(self):
return self.name
@property
def packages(self):
return self.package_set.all()
class Package(models.Model):
destination = models.ForeignKey(Destination, on_delete=models.CASCADE)
package_name = models.CharField(max_length=255)
featured = models.BooleanField(default=False)
price = models.IntegerField()
duration = models.IntegerField(default=5)
discount = models.CharField(max_length=255, default="15% OFF")
discounted_price = models.IntegerField(default=230)
savings = models.IntegerField(default=230)
rating = models.IntegerField(choices=((1, 1),
(2, 2),
(3, 3),
(4, 4),
(5, 5))
)
image = models.ImageField(blank=True)
thumbnail = ImageSpecField(source='image',
processors=[ResizeToFill(100, 50)],
format='JPEG',
options={'quality': 60})
date_created = models.DateField()
def __str__(self):
return self.package_name
class DestinationPackageSerializer(serializers.ModelSerializer):
class Meta:
model = Package
# fields = ['id', 'destination']
fields = '__all__'
class DestinationwithPackageSerializer(serializers.ModelSerializer):
packages = DestinationPackageSerializer(many= True,read_only=True)
class Meta:
model = Destination
fields = ['id', 'name', 'dest_image', 'packages']
# fields = '__all__'
# depth = 1
class DestinationFrontSerializer(serializers.ModelSerializer):
class Meta:
model = Destination
# fields = ['id', 'package']
fields = '__all__'
# depth = 1
My views are:我的观点是:
class DestinationFrontListAPIView(ListAPIView):
queryset = Destination.objects.all().order_by('?')[:4]
# queryset = Package.objects.all().order_by('-date_created')[:4]
serializer_class = DestinationFrontSerializer
class DestinationPackageListAPIView(RetrieveAPIView):
queryset = Destination.objects.all()
serializer_class = DestinationwithPackageSerializer
My urls are:我的网址是:
path('api/destinations', views.DestinationFrontListAPIView.as_view(), name='api-destinations'),
path('api/destinations/<int:pk>', views.DestinationPackageListAPIView.as_view(), name='api-destinations-packages'),
What makes you think that you need destination_id
in the URL for package?是什么让您认为包的 URL 中需要destination_id
? I believe it is not needed.我相信它是不需要的。 Only package_id
should be enough - it is primary key and it uniquely identifies the package.只有package_id
就足够了——它是主键,它唯一地标识了包。
If you do want to include destination_id
to the URL and take it into account when fetching package, you can do it like this:如果您确实希望在 URL 中包含destination_id
并在获取包时将其考虑在内,您可以这样做:
# urls.py
path(
'api/destinations/<int:destination_id>/package/<int:pk>',
views.DestinationPackageItemIView.as_view(),
name='api-destinations-package',
),
# views.py
class DestinationPackageItemIView(RetrieveAPIView):
serializer_class = DestinationPackageSerializer
def get_queryset(self):
destination_id = self.kwargs['destination_id']
return Package.objects.filter(destination_id=destination_id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.