1

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'))
5
  • 1
    you need to define a slug field. Commented Mar 21, 2016 at 13:30
  • I think your url should be http://127.0.0.1:8000/api/club?q=liverpool, so you wouldn't need a slug field and q can be replaced by any other param name. Commented Mar 21, 2016 at 14:34
  • @Gocht When I do this '127.0.0.1:8000/api/club/?abv=MU' it returns everything instead of just the JSON for Manchester United :S Commented Mar 21, 2016 at 14:38
  • @Aceboy1993 It's about your code, when you are retrieving data from db using your GET params, you should show us your view code. Commented Mar 21, 2016 at 14:41
  • I whink you need to redefine your queryset, to filter by your abv param Commented Mar 21, 2016 at 14:50

2 Answers 2

1

I know a simple way by using regex

in the url.py add this regex

url(r'^/api/club/(?P<club_name>[a-zA-Z]+)/$', ....)

then in the views add a method will the club_name variable like this

from django.http import HttpResponse

def getclub(request, club_name):
    teaminfo = ModelName.objects.get(name=club_name) # something like that
    return HttpResponse(teaminfo)

the club_name variable will get the the string from the regex, you can use it in anyway you want.

Here's a simple good reference for this

https://docs.djangoproject.com/en/1.9/intro/tutorial03/

Sign up to request clarification or add additional context in comments.

5 Comments

I did this and it comes up with "detail": "Not found."
I added exactly what you said, you can see my code at the top I've updated it to reflect the changes you suggested
Sorry I forgot to mention to add return HttpResponse(teaminfo) @Aceboy1993
Still doesn't work. I think it's not calling the def getClub function from my urls.py but have no idea why
I think the problem is with ('getClub') in the url.py file, are you sure this is how you call a view in django-rest? @Aceboy1993
0

The issue is that you're using queryset = Team.objects.all() in your view. By nature, this implies that you'll get all of the objects.

I have a similar program, and I use urls.py like this-

...
url(r'^teams/(?P<incoming_team>[^/]+)/$', ('team_stats')),
url(r'^teams/', ('team_stats_all')),

in views it looks something like:

def team_stats(request, incoming_team):
    queryset = Team.objects.get(name=incoming_team)

Obviously your existing view would be used for all. I'd also note you'll need some Try/Except handling, in case you have duplicate teams, or the team you request doesn't exist.

(In practice I don't actually split them out like this into separate views, but it gives you an idea of the logic flow)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.