gpt4 book ai didi

json - 如何在模板中使用 django-rest-framework

转载 作者:行者123 更新时间:2023-12-04 14:26:30 25 4
gpt4 key购买 nike

请多多包涵。我刚学习django-rest-framework .我似乎真的无法捕获它。

模型:

class Day(models.Model):
date = models.DateField(default=date.today)

class ToDo(models.Model):
date = models.ForeignKey(Day)
name = models.CharField(max_length=100)
very_important = models.BooleanField(default=False)
finished = models.BooleanField(default=False)

普通 View :
def home(request):
days = Day.objects.all()

return render(request, 'test.html', {
'days':days
})

创建了一个 serializers.py 文件:
class DaySerializer(serializers.ModelSerializer):
class Meta:
model = Day
field = ('id', 'date')


class ToDoSerializer(serializers.ModelSerializer):
date = DaySerializer()
class Meta:
model = ToDo
field = ('id', 'date', 'name', 'very_important', 'finished')

在这之后我迷路了。假设我去 url('^api/$') ,如何获取Day及其相关对象的json对象?

基本上这就是我想要的显示方式:

enter image description here

现在我如何使用 django-rest-framework 在模板中看起来像上面那样。

更新

我想做这样的事情:
  • 通过访问 url 获取 json 对象(例如:/api/.json)
    [
    {
    "id": 1,
    "date": "2015-06-23",
    "day_todo_set": [
    {
    "id":5
    "name": "Bloom",
    "very_important": True,
    "finished": False
    },
    {
    "id":4
    "name": "REST",
    "very_important": True,
    "finished": True
    }
    ]
    },
    {
    "id": 2,
    "date": "2015-06-22",
    "day_todo_set": [
    {
    "id":3
    "name": "Alarm 1",
    "very_important": True,
    "finished": True
    },
    {
    "id":2
    "name": "Meet up with SIS",
    "very_important": True,
    "finished": False
    },
    {
    "id":1
    "name": "Buy Milk",
    "very_important": True,
    "finished": False
    }
    ]
    }
    ]
  • 操作对象

  • js:
    $(function () {
    var $days = $('#days')

    $.ajax({
    type: 'GET',
    url: '/api/.json',
    success: function(days) {
    $.each(days, function(i, day) {
    $days.append('<li>date: '+ day.date+ '</li>')
    });
    }
    });
    })

    最佳答案

    模型.py:

    class Day(models.Model):
    date = models.DateField(default=date.today)

    def get_todo_list(self):
    return self.day_todo_set.order_by('-id')[:5]

    class ToDo(models.Model):
    date = models.ForeignKey(Day, related_name="day_todo_set")
    name = models.CharField(max_length=100)
    very_important = models.BooleanField(default=False)
    finished = models.BooleanField(default=False)

    在 serializers.py 中
    class ToDoSerializer(serializers.ModelSerializer):

    class Meta:
    model = ToDo
    field = ('id', 'date', 'name', 'very_important', 'finished')

    class DaySerializer(serializers.ModelSerializer):

    day_todo_set = ToDoSerializer(many=True, source="get_todo_list")

    class Meta:
    model = Day
    field = ('id', 'date', 'day_todo_set')

    使用序列化程序编写 Django View 。
    @csrf_exempt
    def day_list(request):
    """
    List all code snippets, or create a new snippet.
    """
    if request.method == 'GET':
    snippets = Day.objects.order_by('-date')[:10]
    serializer = DaySerializer(snippets, many=True)
    return JSONResponse(serializer.data)

    在你的 urls.py
    url(r'^api/$', views.day_list, name='days_list')

    关于json - 如何在模板中使用 django-rest-framework,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30997666/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com