This commit is contained in:
littlewolf 2019-10-25 12:27:38 +03:00
parent bf375f3370
commit abe8eae655
2 changed files with 17 additions and 3 deletions

View File

@ -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,

View File

@ -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):