Query parameters in the URL, with REST Framework

1,894 views
Skip to first unread message

Elio Gerson Clímaco Herrera

unread,
Sep 8, 2014, 3:41:22 PM9/8/14
to django...@googlegroups.com
Hi, everybody.

I, stared using REST Framework few days ago, but I, can't find how create a customized url with parameter, in Django this kind of url is written like this.

url(r'^author/(?P<author>\d+)/books/$', BooksList.as_view(), name = 'books'),

for this 


I, try with:
router.register(r'author/(?P<author>\d+)/books', BooksList, base_name = 'Books')
but this don't work.

Here is my code.

# models.py
class Author(models.Model):
    Name = models.CharField(max_length = 50)

class Book(models.Model):
    Book = models.ForeignKey(Author)
    Title = models.CharField(max_length = 200)

    def __unicode__(self):
        return self.Title


# views.py
class BooksList(viewsets.ModelViewSet):
    model = Book
    serializer_class = BookSerializer
    def get_queryset(self):
        author = self.kwargs['author']
        queryset = Book.objects.filter(Author = author)
        return queryset

# urls.py
router = routers.DefaultRouter()
router.register(r'books', BooksList, base_name = 'Books')

admin.autodiscover()

urlpatterns = patterns('',
url(r'^api/', include('rest_framework.urls', namespace = 'rest_framework')),
)

carlos

unread,
Sep 9, 2014, 1:02:40 PM9/9/14
to django...@googlegroups.com
Hi, maybe you need read documentation about filtering


Cheers

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f2eb0566-b38c-44f4-8814-f93710d03cec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Elio Gerson Clímaco Herrera

unread,
Sep 9, 2014, 1:20:41 PM9/9/14
to django...@googlegroups.com
Yes, Carlos

I, have read documentation, but this don't show how put parameters in router.register like this.
router.register(r'author/(?P<author>\d+)/books', BooksList, base_name = 'Books')

please, any help
Reply all
Reply to author
Forward
0 new messages