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

前端之家收集整理的这篇文章主要介绍了json – 如何在模板中使用django-rest-framework前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
请多多包涵.我刚刚学习 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)

正常观点:

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对象?

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

现在我如何使用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","finished": True
            }
        ]
    },{
        "id": 2,"date": "2015-06-22","day_todo_set": [
            {
                "id":3
                "name": "Alarm 1","finished": True
            },{
                "id":2
                "name": "Meet up with SIS",{
                "id":1
                "name": "Buy Milk","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>')
            });
        }
    });
})

解决方法

model.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','finished')

class DaySerializer(serializers.ModelSerializer):

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

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

使用序列化程序编写django视图.

@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')
原文链接:/js/240682.html

猜你在找的JavaScript相关文章