I'm currently creating an API with the Django REST framework. I am creating it while studying every day, so I will write it here so that I do not forget it.
■ What you want to do When hitting the field specified by Foreign Key in model with URL, I want to get it by specifying the value instead of the ID number!
Below is the result of Rest that is returned when you hit the URL.
python
URL:http://127.0.0.1:8000/my_names
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"my_name": "noge",
}
]
}
URL:http://127.0.0.1:8000/my_name_rules
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"my_name": "1",
"my_name_rule": "noge is rule.",
}
]
}
Example of what you want to do) http://127.0.0.1:8000/my_name_rule/1 <-not '1' I want to get it with http://127.0.0.1:8000/my_name_rule/noge <-'noge'
models.py
from django.db import models
class MyName(models.Model):
my_name = models.CharField(max_length=255, unique=True)
def __unicode__(self):
return self.my_name
class Meta(object):
db_table = 'my_name'
class MyNameRule(models.Model):
my_name = models.ForeignKey(MyName)
my_name_rule = models.CharField(max_length=255)
def __unicode__(self):
return self.my_name_rule
class Meta(object):
db_table = 'my_name_rule'
serializers.py
from rest_framework import serializers
from noge.apps.noge.models import MyName
from noge.apps.noge.models import MyNameRule
class MyNameRuleSerializer(serializers.ModelSerializer):
class Meta(object):
model = MyNameRule
fields = ('my_name', 'my_name_rule')
class MyNameSerializer(serializers.ModelSerializer):
class Meta(object):
model = MyName
fields = ('my_name')
views.py
from rest_framework import viewsets
from rest_framework import generics
from rest_framework.permissions import IsAuthenticated
from noge.apps.noge.models import MyName
from noge.apps.noge.models import MyNameRule
from noge.apps.noge.serializers import MyNameSerializer
from noge.apps.noge.serializers import MyNameRuleSerializer
class MyNameViewSet(viewsets.ModelViewSet):
queryset = MyName.objects.all()
serializer_class = MyNameSerializer
permission_classes = (IsAuthenticated,)
class MyNameRuleViewSet(viewsets.ModelViewSet):
queryset = MyNameRule.objects.all()
serializer_class = MyNameRuleSerializer
permission_classes = (IsAuthenticated,)
class MyNameRuleFilterViewSet(generics.ListAPIView):
serializer_class = MyNameRuleSerializer
def get_queryset(self):
query_my_name = self.kwargs['my_name']
return MyNameRule.objects.filter(my_name__my_name=query_my_name)
urls.py
from rest_framework import routers
from noge.apps.noge import views
from noge.apps.noge.views import MyNameRuleFilterViewSet
router = routers.DefaultRouter(trailing_slash=False)
router.register(r'my_names', views.MyNameViewSet)
router.register(r'my_name_rules', views.MyNameRuleViewSet)
urlpatterns = patterns('noge.apps.noge.views',
url(r'^v1/my_name_rules/(?P<my_name>\w+)/$',MyNameRuleFilterViewSet.as_view()),
url(r'^v1/', include(router.urls)),
url(r'^$', 'index', name='index'),)
You should be able to do this! That's it (`・ ω ・ ´) ゞ