Here in the above picture, I have all the packages shown for single destination id ie 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. 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? I believe it is not needed. Only package_id
should be enough - it is primary key and it uniquely identifies the package.
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:
# 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.