Django REST framework is a powerful and flexible toolkit for building Web APIs.
Some reasons you might want to use REST framework:
-
The Web browsable API is a huge usability win for your developers.
-
Authentication policies including packages for OAuth1a and OAuth2.
-
Serialization that supports both ORM and non-ORM data sources.
-
Customizable all the way down – just use regular function-based views if you don’t need the more powerful features.
-
-
Used and trusted by internationally recognised companies including Mozilla, Red Hat, Heroku, and Eventbrite.
之前虽然也用了Django REST framework 但是序列化函数基本都是自己写的,并没有用框架带的序列化函数。这次不想在搞的那么麻烦,于是使用Django REST framework带的序列化函数。
但是在序列化foreignkey的时候却发现只有id,其余的数据没有。
model定义:
class PlayerGoodsItem(models.Model):
create = models.DateTimeField(default=timezone.now, help_text='创建时间')
update = models.DateTimeField(auto_now=True, help_text='修改时间')
goods_item = models.ForeignKey(GoodsItem, related_name='goodsitem_playergoodsitem', help_text='商品信息',
on_delete=models.CASCADE)
序列化代码:
class PlayerGoodsItemSerializer(serializers.ModelSerializer):
class Meta:
model = PlayerGoodsItem
fields = "__all__"