Ich erstelle derzeit eine API mit dem Django REST-Framework. Ich schreibe es jeden Tag während des Studiums, also werde ich es hier schreiben, damit ich es nicht vergesse.
■ Was Sie tun möchten Wenn ich das vom Fremdschlüssel im Modell angegebene Feld mit der URL treffe, möchte ich es erhalten, indem ich den Wert anstelle der ID-Nummer gebe!
Unten sehen Sie das Ergebnis von Rest, das zurückgegeben wird, wenn Sie auf die URL klicken.
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.",
}
]
}
Beispiel für das, was Sie tun möchten) http://127.0.0.1:8000/my_name_rule/1 <-not '1' Ich möchte es mit http://127.0.0.1:8000/my_name_rule/noge <- 'noge' erhalten
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'),)
Sie sollten dazu in der Lage sein! Das war's (`・ ω ・ ´) ゞ