У меня есть этот модельный город, и я пытаюсь получить данные из внешних таблиц
models.py:
class City(BaseModel):
name = models.CharField(_("City Name"), max_length=80, null=False, blank=False)
state_name = models.ForeignKey(State, to_field="uid", on_delete=models.DO_NOTHING, max_length=55, null=False, blank=False)
city_type = models.ForeignKey(TypeOfCity, to_field="key", on_delete=models.DO_NOTHING, max_length=15, null=False, blank=False)
city_tier = models.ForeignKey(CityTier, to_field="key", on_delete=models.DO_NOTHING, max_length=10, null=False, blank=False)
status = models.SmallIntegerField(_("Status: 1 for Active; 0:Inactive"), default=1)
class TypeOfCity(models.Model):
key = models.CharField(verbose_name=_("key"), max_length=15, unique=True)
value = models.CharField(verbose_name=_("value"), unique=True, max_length=15)
status = models.SmallIntegerField(_("status:1 for Active; 0: Inactive"), default=1)
views.py:
@api_view(['POST'])
def cityFetch(request):
try:
data =decode_data(request.data.copy())
try:
queryset = City.objects.filter(uid=data['uid']).values('name','city_type','city_type__value','status')
serializer_obj = CitySerializer(queryset,many=True)
return CustomeResponse(request=request, comment="Get Single City", message="Get Single City", data=json.dumps(serializer_obj.data, cls=UUIDEncoder), status=status.HTTP_200_OK)
except City.DoesNotExist:
return CustomeResponse(request=request, comment="City Not Found", message="City Not Found",data=json.dumps({}, cls=UUIDEncoder), status=status.HTTP_400_BAD_REQUEST, validate_errors=1)
except Exception as e:
print(e)
error_str = UID_KEY_IS_MISSING if type(e) is KeyError else UID_IS_NOT_VALID
return CustomeResponse(request=request, log_data=json.dumps(str(e), cls=UUIDEncoder), comment=error_str, message=error_str, data=json.dumps({}, cls=UUIDEncoder), status=status.HTTP_400_BAD_REQUEST, validate_errors=1)
Я получаю этот набор запросов:
<QuerySet [{'name': 'test', 'city_type': 'normal_city', 'city_type_id__value': 'Normal City', 'status': 1}]>
, но, возможно, он пытается найти ключ, и я получаю эту ошибку:
'str' object has no attribute 'key'
def decode_data(data):
try:
if data.get('encoded_data', None)=="yes":
try:
output = ast.literal_eval(decrypt(data.get('data', None)).decode('utf-8'))
except Exception as e:
output = json.loads(decrypt(data.get('data', None)).decode('utf-8'))
else:
return output
except Exception as e:
output = data.get('data', None)
return output
............... .................................................. .................