TheRapture87
TheRapture87

Reputation: 1423

JSON/Django http request

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'))

Upvotes: 1

Views: 242

Answers (2)

Khaled Al-Ansari
Khaled Al-Ansari

Reputation: 3970

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/

Upvotes: 1

Withnail
Withnail

Reputation: 3178

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)

Upvotes: 0

Related Questions