I want to add a detail view in addition to the standard router views, that
uses a UUID field instead of pk
I have a viewset for standard crud operations for a django model
eg: the model is a list of weather sites
class Weather(models.Model):
name = models.CharField(max_length=100)
latitude = models.FloatField()
longitude = models.FloatField()
ambient_temp = models.FloatField()
shared_key = models.UUIDField()
owner =models.ForeignKey(User)
so the views and serializers are very basic
class WeatherViewSet(viewsets.ModelViewSet):
queryset = Weather.objects.all()
class WeatherSerializer(serializers.ModelSerializer):
class Meta:
model = Weather
fields = '__all__'
Now I just want to add a detail view that returns the
`Weather.objects.get(shared_key=<key>)` instead of by pk
can I just add a `@detail_route` to a method in the view?
class WeatherViewSet(viewsets.ModelViewSet):
queryset = Weather.objects.all()
@detail_route(lookup_field='shared_key',lookup_regex='[0-9a-f]{32}'
def get_weather_by_key(self, request, key=None):
return Response(Weather.objects.get(shared_key=key)
thanks,
Mark
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.