Please bear with me. I am just learning django-rest-framework. And I really can't seem to grab it.
请多多包涵。我只是在学习django-rest-framework。我真的无法抓住它。
model:
模型:
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)
normal view:
正常观点:
def home(request):
days = Day.objects.all()
return render(request, 'test.html', {
'days':days
})
Created a serializers.py file:
创建了一个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')
After this I am lost. Suppose I go to url('^api/$')
, how do I get the json objects of Day and its related object?
在此之后我迷失了。假设我转到url('^ api / $'),如何获取Day及其相关对象的json对象?
Basically this is how I want to be displayed:
基本上这就是我想要显示的方式:
Now how can I work with django-rest-framework to look like the above in the template.
现在我如何使用django-rest-framework看起来像模板中的上述内容。
update
更新
I would like to do something like this:
我想做这样的事情:
Get the json objects by going to a url (eg: /api/.json)
通过转到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
}
]
}
]
Manipulate objects
操纵对象
js:
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>')
});
}
});
})
7
model.py:
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)
In serializers.py
在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')
Write a django view using your serializer.
使用序列化程序编写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)
In your urls.py
在你的urls.py中
url(r'^api/$', views.day_list, name='days_list')
0
rest framework is not aimed to serve html that is displayed in a browser. It is aimed to serve data to another service (a mobile device, an heavy client, an other server, etc.)
rest框架的目的不是提供在浏览器中显示的html。它旨在将数据提供给另一个服务(移动设备,重型客户端,其他服务器等)
First line of the documentation:
文档的第一行:
Django REST framework is a powerful and flexible toolkit that makes it easy to build Web APIs.
Django REST框架是一个功能强大且灵活的工具包,可以轻松构建Web API。
If you want to display the result in a browser, use templates and do not use rest
如果要在浏览器中显示结果,请使用模板,不要使用休息
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2015/06/23/65b3b385a57f225c075d650ff7deda2d.html。