Django rest framework获取除POST pk之外的所有数据 [英] Django rest framework GET all data but POST pk
问题描述
我正在创建一个API,并试图弄清楚如何将我的 Shipping
模型中的所有数据返回到GET方法中,但仍通过POST方法发送pk.
I am creating an API and trying to figure out how to return all data from my Shipping
model into the GET method, but still sending the pk through the POST method.
我已经阅读了一些解决方案和此处,但它没有为了解决我的整个问题,有时我需要从API中获得两种不同的行为,客户端只需要发送主键即可.
I already read some solutions here and here, but it didn't solve my entire problem, at some point I need two different behaviors from my API, where the client need to send only the primary key.
我希望这在我的GET /Shipping
中:
I expect this in my GET /Shipping
:
[{
"pk": 1,
...
"city_of_origin": {
"pk": 1,
"name": "San Francisco",
"state": {
"pk": 1,
"initial": "CA",
"name": "California"
}
},
"destination_cities": [
{
"pk": 2,
"name": "San Jose",
"state": {
"pk": 1,
"initial": "CA",
"name": "California"
}
},{
"pk": 3,
"name": "Los Angeles",
"state": {
"pk": 1,
"initial": "CA",
"name": "California"
}
}
]
}]
这在我的POST中:
[{
"pk": 1,
...
"city_of_origin": 1,
"destination_cities": [2, 3]
}]
我一直在尝试更改我的 serializers.py
:
I've been trying to change my serializers.py
:
class StateSerializer(serializers.ModelSerializer):
class Meta:
model = State
class CitySerializer(serializers.ModelSerializer):
state = StateSerializer()
class Meta:
model = City
class ShippingSerializer(serializers.ModelSerializer):
city_of_origin = CitySerializer()
destination_cities = CitySerializer(many=True)
class Meta:
model = Shipping
返回所有数据效果很好,但是它更改了我所有的API根表,迫使创建了一个城市和一个嵌套在运输"中的州,在更改序列化程序之前,我在城市中有一个下拉菜单.但是,此下拉列表的显示是我在POST表单上期望的行为.
It worked well returning all data, however it changed all my API Root forms, forcing to create a city and a state nested to my Shipping, where I had a dropdown menu with the cities before I changed my serializer. However the exhibition of this dropdown is the behavior I expect on the POST Form.
这是我的 models.py
:
class Shipping(models.Model):
city_of_origin = models.ForeignKey(City, related_name='origin', default=None)
destination_cities = models.ManyToManyField(City, related_name='destiny', default=None)
class City(models.Model):
name = models.CharField(blank=False, null=True, max_length=255)
state = models.ForeignKey(State, null=True)
def __str__(self):
return self.name
class State(models.Model):
name = models.CharField(blank=False, null=True, max_length=255)
initial = models.CharField(max_length=2, blank=False, null=True)
def __str__(self):
return self.name
在此先感谢你们能为我提供的所有帮助.
I want to thank in beforehand all the help you guys can provide me.
我正在使用Django 1.9.5和Django rest框架3.3.3
I'm using Django 1.9.5 and Django rest framework 3.3.3
推荐答案
如果 get
和 post
由同一个其他api视图处理,我认为您正在使用类似于 ViewSet
(或适当混合的GenericAPIView).您的 ViewSet
将使用其他序列化程序进行获取和发布.
If get
and post
are being handled by the same rest api view, I think you are using something like a ViewSet
(or an appropriately mixed GenericAPIView). Your ViewSet
will use a different serializer for getting and for posting.
要获取/列出信息,请使用已经创建的名称(让我们将其重命名):
For getting/listing you will use the one you already created (let's rename it):
class ShippingGetSerializer(serializers.ModelSerializer):
city_of_origin = CitySerializer()
destination_cities = CitySerializer(many=True)
class Meta:
model = Shipping
发布:
class ShippingPostSerializer(serializers.ModelSerializer):
city_of_origin = serializers.PrimaryKeyRelatedField()
destination_cities = serializers.PrimaryKeyRelatedField(many=True)
class Meta:
model = Shipping
您的ViewSet将具有 get_serializer()
的定义,如下所示:
Your ViewSet would have a definition of get_serializer()
like this:
class ShippingViewSet(viewsets.ModelViewSet):
queryset = Shipping.objects.all()
def get_serializer_class(self):
if self.request.method == 'POST':
return ShippingPostSerializer
return ShippingGetSerializer
如果您对get和post入口点使用两个不同的视图,请创建每个视图,分别为它们分配 serializer_class
class属性给我编写的相应序列化器.
If you are using two different views for the get and the post entry point, create each one assigning them the serializer_class
class attribute to the appropriate serializer as I wrote.
这篇关于Django rest framework获取除POST pk之外的所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!