Reputation: 13206
I am getting the following response from my REST API at http://127.0.0.1:8000/api/category/:
[
{
"id": "17442811-3217-4b67-8c2c-c4ab762460d6",
"title": "Hair and Beauty"
},
{
"id": "18a136b5-3dc4-4a98-97b8-9604c9df88a8",
"title": "Plumbing"
},
{
"id": "2f029642-0df0-4ceb-9058-d7485a91bfc6",
"title": "Personal Training"
}
]
If I wanted to access, a single record, I presume that I would need to go to http://127.0.0.1:8000/api/category/17442811-3217-4b67-8c2c-c4ab762460d6 to access:
[
{
"id": "17442811-3217-4b67-8c2c-c4ab762460d6",
"title": "Hair and Beauty"
}
]
However, when I attempt this, it returns all of the records. How can I resolve this? This is my code so far:
urls.py
urlpatterns = [
url(r'^category/', views.CategoryList.as_view(), name="category_list"),
url(r'^category/?(?P<pk>[^/]+)/$', views.CategoryDetail.as_view(), name="category_detail")
]
views.py
class CategoryList(generics.ListAPIView):
"""
List or create a Category
HTTP: GET
"""
queryset = Category.objects.all()
serializer_class = CategorySerializer
class CategoryDetail(generics.RetrieveUpdateDestroyAPIView):
"""
List one Category
"""
serializer_class = CategorySerializer
serializers.py
class CategorySerializer(serializers.ModelSerializer):
"""
Class to serialize Category objects
"""
class Meta:
model = Category
fields = '__all__'
read_only_fields = ('id')
models.py
class Category(models.Model):
"""
Category model
"""
id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
title = models.CharField(max_length=255)
def __str__(self):
return "%s" % (self.title)
Upvotes: 1
Views: 4341
Reputation: 599628
Your first regex, r'^category/'
, matches both a URL with and without a UUID.
You should anchor it at the end:
r'^category/$'
Additionally/alternatively, you can swap the order of those URL definitions, as Django will take the first one it matches.
Upvotes: 2