I've written some django code that allows me to request some data and returns it into a JSON format.
I request: http http://127.0.0.0.8000/api/club
[
{
"name": "Liverpool FC",
"abv": "LFC",
"stadium": "Anfield",
"manager": "Jurgen Klopp",
"trophies": "50"
},
{
"name": "Manchester City",
"abv": "MC",
"stadium": "Etihad",
"manager": "Manuel Pellegrini",
"trophies": "14"
},
{
"name": "Manchester United",
"abv": "MU",
"stadium": "Old Trafford",
"manager": "Louis Van Gaal",
"trophies": "46"
}
]
Is it possible to request only "Liverpool FC"'s data such that the request only returns. I can do this by http http://127.0.0.0.8000/api/club/1/ but I'd rather be able to type in a team name like http http://127.0.0.0.8000/api/club/liverpool/
{
"name": "Liverpool FC",
"abv": "LFC",
"stadium": "Anfield",
"manager": "Jurgen Klopp",
"trophies": "50"
}
Edit: Added two py files
views.py
# Create your views here.
class FishViewSet(viewsets.ModelViewSet):
# this fetches all the rows of data in the Fish table
queryset = Fish.objects.all()
serializer_class = FishSerializer
# Create your views here.
class TeamViewSet(viewsets.ModelViewSet):
# this fetches all the rows of data in the Fish table
queryset = Team.objects.all()
serializer_class = TeamSerializer
# Create your views here.
class ClubViewSet(viewsets.ModelViewSet):
# this fetches all the rows of data in the Fish table
queryset = Club.objects.all()
serializer_class = ClubSerializer
def getClub(request, club_name):
queryset = Club.objects.get(name=club_name)
models.py
class Fish(models.Model):
name = models.CharField(max_length=255)
created = models.DateTimeField('auto_now_add=True')
active = models.BooleanField()
class Team(models.Model):
starting = models.CharField(max_length=255)
captain = models.CharField(max_length=255)
value = models.CharField(max_length=255)
fixtures = models.CharField(max_length=255)
position = models.CharField(max_length=255)
class Club(models.Model):
name = models.CharField(max_length=255)
abv = models.CharField(max_length=255)
stadium = models.CharField(max_length=255)
manager = models.CharField(max_length=255)
trophies = models.CharField(max_length=255)
urls.py
router = routers.DefaultRouter()
#makes sure that the API endpoints work
router.register(r'api/fishes', views.FishViewSet)
router.register(r'api/teams', views.TeamViewSet)
router.register(r'api/club', views.ClubViewSet)
admin.autodiscover()
urlpatterns = router.urls
url(r'^admin/', include(admin.site.urls)),
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
url(r'^/api/club/(?P<club_name>[a-zA-Z]+)/$', ('getClub'))
http://127.0.0.1:8000/api/club?q=liverpool, so you wouldn't need a slug field andqcan be replaced by any other param name.abvparam