Django REST框架:定义嵌套对象中的字段? [英] Django REST Framework: define fields in nested object?
本文介绍了Django REST框架:定义嵌套对象中的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发现事件发生在地点:
class Event(models.Model):
title = models .CharField(max_length = 200)
date_published = models.DateTimeField('published date',default = datetime.now,blank = True)
date_start = models.DateTimeField('start date')
date_end = models.DateTimeField('end date')
def __unicode __(self):
return self.title
description = models.TextField()
price = models.IntegerField (null = True,blank = True)
tags = TaggableManager()
location = models.ForeignKey(Location,blank = False)
class位置(models.Model)
location_title = models.CharField(max_length = 200)
location_date_published = models.DateTimeField('published date',default = datetime.now,blank = True)
location_latitude = models.CharField(max_length = 200)
location_longitude = models.CharField(max_length = 200)
location_address = models.Cha rField(max_length = 200)
location_city = models.CharField(max_length = 200)
location_zipcode = models.CharField(max_length = 200)
location_state = models.CharField(max_length = 200)
location_country = models.CharField(max_length = 200)
location_description = models.TextField()
def __unicode __(self):
return u'%s'%(self.location_title)
我可以通过以下方式获得所有结果:
class EventSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.Field()
class Meta:
model = Event
depth = 2
fields =('url','id','title','date_start','date_end','description','price','location')
哪些输出:
{
url:http:// localhost:8000 / api / event / 3 /,
id:3,
title ,
date_start:2013-03-10T20:19:00Z,
date_end:2013-03-10T20:19:00Z,
description fgdgdfg,
price:10,
location:{
id:2,
location_title:Mighty,
location_date_published :2013-03-10T20:16:00Z,
location_latitude:37.767475,
location_longitude:-122.406878,
location_address:119犹他州旧金山,CA 94103,USA,
location_city:旧金山,
location_zipcode:94103,
location_state:California $ blocation_country:美国,
location_description:一些地方
}
},
但是,我不希望它抓住所有字段,因为我不需要它们。我如何定义从我的嵌套对象中检索哪些字段?谢谢!
解决方案
序列化程序可以嵌套,所以做这样的事情...
pre>
class LocationSerializer(serializers.ModelSerializer):
class Meta:
model = Location
fields =(...)
class EventSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.Field()
location = LocationSerializer()
class Meta:
model =事件
fields =('url','id','title','date_start','date_end','description','price','location')
I got events that happen at locations:
class Event(models.Model):
title = models.CharField(max_length=200)
date_published = models.DateTimeField('published date',default=datetime.now, blank=True)
date_start = models.DateTimeField('start date')
date_end = models.DateTimeField('end date')
def __unicode__(self):
return self.title
description = models.TextField()
price = models.IntegerField(null=True, blank=True)
tags = TaggableManager()
location = models.ForeignKey(Location, blank=False)
class Location(models.Model):
location_title = models.CharField(max_length=200)
location_date_published = models.DateTimeField('published date',default=datetime.now, blank=True)
location_latitude = models.CharField(max_length=200)
location_longitude = models.CharField(max_length=200)
location_address = models.CharField(max_length=200)
location_city = models.CharField(max_length=200)
location_zipcode = models.CharField(max_length=200)
location_state = models.CharField(max_length=200)
location_country = models.CharField(max_length=200)
location_description = models.TextField()
def __unicode__(self):
return u'%s' % (self.location_title)
I can get the results of all via:
class EventSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.Field()
class Meta:
model = Event
depth = 2
fields = ('url','id','title','date_start','date_end','description', 'price', 'location')
Which outputs:
{
"url": "http://localhost:8000/api/event/3/",
"id": 3,
"title": "Testing",
"date_start": "2013-03-10T20:19:00Z",
"date_end": "2013-03-10T20:19:00Z",
"description": "fgdgdfg",
"price": 10,
"location": {
"id": 2,
"location_title": "Mighty",
"location_date_published": "2013-03-10T20:16:00Z",
"location_latitude": "37.767475",
"location_longitude": "-122.406878",
"location_address": "119 Utah St, San Francisco, CA 94103, USA",
"location_city": "San Francisco",
"location_zipcode": "94103",
"location_state": "California",
"location_country": "United States",
"location_description": "Some place"
}
},
However, I don't want it to grab all fields, as I don't need all of them. How can I define what fields should be retrieved from my nested object? Thanks!
解决方案
Serializers can be nested, so do something like this...
class LocationSerializer(serializers.ModelSerializer):
class Meta:
model = Location
fields = (...)
class EventSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.Field()
location = LocationSerializer()
class Meta:
model = Event
fields = ('url','id','title','date_start','date_end','description', 'price', 'location')
这篇关于Django REST框架:定义嵌套对象中的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文