From abe8eae6551ad4c6a5cc029869f001d73009b7ad Mon Sep 17 00:00:00 2001 From: littlewolf Date: Fri, 25 Oct 2019 12:27:38 +0300 Subject: [PATCH] Tmp --- apps/location/transfer_data.py | 7 +++++-- apps/transfer/serializers/location.py | 13 ++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/apps/location/transfer_data.py b/apps/location/transfer_data.py index 745ff23c..885c47e0 100644 --- a/apps/location/transfer_data.py +++ b/apps/location/transfer_data.py @@ -12,9 +12,13 @@ def transfer_countries(): else: pprint(f"Country serializer errors: {serialized_data.errors}") + def transfer_regions(): regions_without_subregion_queryset = Cities.objects.\ - exclude(subregion_code__isnull=False).values('region_code', 'country_code_2', 'subregion_code').distinct() + exclude(subregion_code__isnull=True).\ + exclude(region_code__isnull=True).\ + exclude(country_code_2__isnull=True).\ + values('region_code', 'country_code_2', 'subregion_code').distinct() serialized_without_subregion = RegionSerializer(data=list(regions_without_subregion_queryset.values()), many=True) if serialized_without_subregion.is_valid(): @@ -23,7 +27,6 @@ def transfer_regions(): pprint(f"Parent regions serializer errors: {serialized_without_subregion.errors}") - data_types = { "dictionaries": [ transfer_countries, diff --git a/apps/transfer/serializers/location.py b/apps/transfer/serializers/location.py index a6d000ef..61df6baf 100644 --- a/apps/transfer/serializers/location.py +++ b/apps/transfer/serializers/location.py @@ -27,7 +27,7 @@ class CountrySerializer(serializers.ModelSerializer): class RegionSerializer(serializers.ModelSerializer): region_code = serializers.CharField() - subregion_code = serializers.CharField() + subregion_code = serializers.CharField(allow_null=True) country_code_2 = serializers.CharField() class Meta: @@ -39,6 +39,17 @@ class RegionSerializer(serializers.ModelSerializer): ) def validate(self, data): + data['code'] = data.pop('region_code') + if "country_code_2" in data and data["country_code_2"] is not None: + try: + country = Country.objects.get(code=data['country_code_2']) + data.country = country + del (data['country_code_2']) + + except Country.DoesNotExist as e: + print(f"Country error: {e}") + + del(data['subregion_code']) return data def create(self, validated_data):