I'm building an SMS API that talks to a Django database, which is a list of contact information for several hundred people. The fields are as follows: first name, last name, phone number and job title.
I'm getting responses when I use this url on my local server:
http://localhost:8000/sources/id
What I'd like to do is make requests to the same database using this url:
http://localhost:8000/sources/first_name-last_name
I've investigated multiple questions about field lookups, but there hasn't been anything helpful. Here's what my serializers.py look like:
from rest_framework import serializers, viewsets
from text_source.models import Source
class SourceSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Source
fields = ('id','first_name','last_name','title','organization','city','cell_phone','office_phone')
lookup_field = 'first_name'
class SourceViewSet(viewsets.ModelViewSet):
queryset = Source.objects.all()
serializer_class = SourceSerializer
lookup_field = ('first_name')
I'm not sure if using /first_name-last_name as an end point for the url is best practice, but, in theory for what I'm doing, it will work.
Ideally, I'd like for someone to type FIRSTNAME LASTNAME in the text and have the API return the correct information by connecting the full name to the ID in the database. Any tips to accomplishing that would be greatly appreciated.
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.views.generic.base import TemplateView
from django.contrib.auth.models import User
from rest_framework import routers, serializers, viewsets
from app import views
from app.serializers import SourceSerializer, SourceViewSet
router = routers.DefaultRouter()
router.register(r'sources', SourceViewSet)
urlpatterns = [
url(r'^page/$', TemplateView.as_view(template_name='base.html')),
url(r'^page/', include(router.urls)),
url(r'^admin/', include(admin.site.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^page/incoming/message$', views.incoming_message)
]
lookup_field = ('first_name')in round brackets?MultipleFieldLookupMixin). Then make sure you updateurls.pyto match and name the keyword arguments correctly (name & surname)