Python 如何在同时创建父级时将外键传递给序列化程序?
作为前言,我是一名初学者,我意识到我的惯例可能并不完全符合标准,因此如果您能纠正我犯的任何错误,我将不胜感激 目前,调用my API时使用的是:Python 如何在同时创建父级时将外键传递给序列化程序?,python,django,Python,Django,作为前言,我是一名初学者,我意识到我的惯例可能并不完全符合标准,因此如果您能纠正我犯的任何错误,我将不胜感激 目前,调用my API时使用的是: http:127.0.0.1:8000/weather/<latitude>,<longitude> 型号.py def get(self, request, *args, **kwargs): # Process latitude and longitude coordinates from URL coor
http:127.0.0.1:8000/weather/<latitude>,<longitude>
型号.py
def get(self, request, *args, **kwargs):
# Process latitude and longitude coordinates from URL
coordinates = kwargs.pop('location', None).split(",")
latitude = coordinates[0]
longitude = coordinates[1]
# Retrieve weather data.
forecast = get_weather(latitude, longitude)
currently = forecast['currently']
# Serialize and confirm validity of data.
location_serializer = LocationSerializer(data=forecast)
if not location_serializer.is_valid():
return Response(location_serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
currently_serializer = CurrentlySerializer(data=currently)
if not currently_serializer.is_valid():
return Response(currently_serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
response = location_serializer.data + currently_serializer.data
return Response(response, status=status.HTTP_200_OK)
class Location(models.Model):
... some fields
class DataPoint(models.Model):
... some fields
class Meta:
abstract = True
class Currently(DataPoint):
location = models.ForeignKey(Location, on_delete=models.CASCADE)
class LocationSerializer(serializers.ModelSerializer):
class Meta:
model = Location
fields = '__all__'
class CurrentlySerializer(serializers.ModelSerializer):
class Meta:
model = Currently
fields = '__all__'
def get_weather(latitude, longitude):
response = requests.get('https://api.darksky.net/forecast/' +
settings.WEATHER_API_KEY + '/' +
latitude + ',' +
longitude + '/')
return response.json()
序列化程序.py
def get(self, request, *args, **kwargs):
# Process latitude and longitude coordinates from URL
coordinates = kwargs.pop('location', None).split(",")
latitude = coordinates[0]
longitude = coordinates[1]
# Retrieve weather data.
forecast = get_weather(latitude, longitude)
currently = forecast['currently']
# Serialize and confirm validity of data.
location_serializer = LocationSerializer(data=forecast)
if not location_serializer.is_valid():
return Response(location_serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
currently_serializer = CurrentlySerializer(data=currently)
if not currently_serializer.is_valid():
return Response(currently_serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
response = location_serializer.data + currently_serializer.data
return Response(response, status=status.HTTP_200_OK)
class Location(models.Model):
... some fields
class DataPoint(models.Model):
... some fields
class Meta:
abstract = True
class Currently(DataPoint):
location = models.ForeignKey(Location, on_delete=models.CASCADE)
class LocationSerializer(serializers.ModelSerializer):
class Meta:
model = Location
fields = '__all__'
class CurrentlySerializer(serializers.ModelSerializer):
class Meta:
model = Currently
fields = '__all__'
def get_weather(latitude, longitude):
response = requests.get('https://api.darksky.net/forecast/' +
settings.WEATHER_API_KEY + '/' +
latitude + ',' +
longitude + '/')
return response.json()
services.py
def get(self, request, *args, **kwargs):
# Process latitude and longitude coordinates from URL
coordinates = kwargs.pop('location', None).split(",")
latitude = coordinates[0]
longitude = coordinates[1]
# Retrieve weather data.
forecast = get_weather(latitude, longitude)
currently = forecast['currently']
# Serialize and confirm validity of data.
location_serializer = LocationSerializer(data=forecast)
if not location_serializer.is_valid():
return Response(location_serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
currently_serializer = CurrentlySerializer(data=currently)
if not currently_serializer.is_valid():
return Response(currently_serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
response = location_serializer.data + currently_serializer.data
return Response(response, status=status.HTTP_200_OK)
class Location(models.Model):
... some fields
class DataPoint(models.Model):
... some fields
class Meta:
abstract = True
class Currently(DataPoint):
location = models.ForeignKey(Location, on_delete=models.CASCADE)
class LocationSerializer(serializers.ModelSerializer):
class Meta:
model = Location
fields = '__all__'
class CurrentlySerializer(serializers.ModelSerializer):
class Meta:
model = Currently
fields = '__all__'
def get_weather(latitude, longitude):
response = requests.get('https://api.darksky.net/forecast/' +
settings.WEATHER_API_KEY + '/' +
latitude + ',' +
longitude + '/')
return response.json()
您需要检索当前要附加到
的位置
实例,并将位置
主键分配给数据
def get(self, request, *args, **kwargs):
# Process latitude and longitude coordinates from URL
coordinates = kwargs.pop('location', None).split(",")
latitude = coordinates[0]
longitude = coordinates[1]
# Retrieve the Location by latitude and longitude
location, created = Location.objects.get_or_create(latitude=latitude, longitude=longitude, defaults={'other': 'fields', 'to': 'add', 'on': 'create')
# Retrieve weather data.
forecast = get_weather(latitude, longitude)
currently = forecast['currently']
# Assign location.pk to currently data
currently['location'] = location.pk
# Serialize and confirm validity of data.
location_serializer = LocationSerializer(data=forecast)
if not location_serializer.is_valid():
return Response(location_serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
currently_serializer = CurrentlySerializer(data=currently)
if not currently_serializer.is_valid():
return Response(currently_serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
response = location_serializer.data + currently_serializer.data
return Response(response, status=status.HTTP_200_OK)
你能给我一个forecast=get_weather(latitude,longitude)
的输出吗?我刚刚在services.py中编辑了这篇文章。我使用的是DarkSky,所以完整的响应格式在他们的文档中:是的,它只是Dark Sky的API返回的一块json。比如:{“经度”:x…data}我只是不知道为什么要使用ForeignKey
字段,如果你需要传递它..对不起,我的意思是我想让Location中的父键填充我当前模型中的“Location”字段。在我的情况下,在查询数据库时,该位置可能还不存在。如果是这种情况,有没有办法让我创建一个位置条目,然后像您那样分配Location.pk而不是引发Http404?嘿,您可以使用Location.objects.get\u或\u create(…)
来获取位置(如果存在),或者创建位置(如果不存在)。我更新了我的答案来反映这一点。很好,效果很好。现在唯一的问题是LocationSerializer正在创建第二行,而不是更新由Location.objects.get\u或\u create(…)
创建的行。实际上我已经解决了这个问题。我只需要将位置传递到序列化程序中。