python - How to pass foreign key to serializer when creating parent at the same time? -


just preface, i'm beginner , realise conventions may not standard grateful if correct me i'm doing wrong.

currently, api called using:

http:127.0.0.1:8000/weather/<latitude>,<longitude> 

i pulling weather data api, want store in database @ same time. represent weather, have 2 models, location , currently hold coordinates , weather information. in case, parent location.

my issue don't know how pass location foreign key currentlyserializer. in code below i'm not passing in @ , receive obvious "location field required" error.

views.py

def get(self, request, *args, **kwargs):     # process latitude , longitude coordinates url     coordinates = kwargs.pop('location', none).split(",")     latitude = coordinates[0]     longitude = coordinates[1]      # retrieve weather data.     forecast = get_weather(latitude, longitude)     = forecast['currently']      # serialize , 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) 

models.py

class location(models.model):     ... fields   class datapoint(models.model):     ... fields     class meta:         abstract = true   class currently(datapoint):     location = models.foreignkey(location, on_delete=models.cascade) 

serializers.py

class locationserializer(serializers.modelserializer):     class meta:         model = location         fields = '__all__'   class currentlyserializer(serializers.modelserializer):     class meta:         model =         fields = '__all__' 

services.py

def get_weather(latitude, longitude):     response = requests.get('https://api.darksky.net/forecast/' +                         settings.weather_api_key + '/' +                         latitude + ',' +                         longitude + '/')     return response.json() 

you need retrieve location instance want attach currently, , assign locations primary key data.

def get(self, request, *args, **kwargs):     # process latitude , longitude coordinates url     coordinates = kwargs.pop('location', none).split(",")     latitude = coordinates[0]     longitude = coordinates[1]     # retrieve location latitude , 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)     = forecast['currently']      # assign location.pk data     currently['location'] = location.pk      # serialize , 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) 

Comments

Popular posts from this blog

Add new key value to json node in java -

javascript - Highcharts Synchronized charts with missing data points -

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -