使用Djang获取Restful路径上的特定对象

2024-05-23 22:08:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码:

美国石油学会/网址.py

from django.conf.urls import url
from api import views

urlpatterns = [
    url(r"^entries", views.EntryList.as_view(), name="api-entries-list"),
    url(r"^entries/(?P<pk>[0-9]+)/$", views.EntrySingle.as_view(), name="api-entry")
]

美国石油学会/视图.py

^{pr2}$

美国石油学会/序列化程序.py

from blog.models import *
from cvitae.models import *
from activities.models import *

from rest_framework import serializers

class EntrySerializer(serializers.ModelSerializer):
    class Meta:
        model = Entry
        fields = ("title", "slug")

我有一个名为blog的应用程序,它包含Entry模型,简称为titlecontent和{}。我想使用Django Rest框架来获取、放置或删除我的条目。代码如上所示。当我触发/api/entries时,没关系,它给出数据库中所有的Entry模型实例。在

[
    {
        "title": "And Maybe This is Another One",
        "slug": "and-maybe-this-is-another-one"
    },
    {
        "title": "Another Topic Here",
        "slug": "another-topic-here"
    },
    {
        "title": "An Example Title",
        "slug": "an-example-title"
    }
]

但是,当我确实想要一个Entry实例时,我仍然得到相同的输出,这意味着我仍然得到所有对象。我不知道我哪里做错了。在


环境

  • django 1.9.5款
  • python 3.5.1
  • djangorestframework 3.3.3

Tags: django代码frompyimportapiurltitle