python - Django not recognize UUID Foreign Key -
i had current model:
class apps(models.model): """ apps são os aplicativos que terão financeiro gerido, uma empresa/usuario pode ter multiplos apps conectados """ app_code = models.uuidfield(auto_created=true, default=uuid.uuid4, unique=true, primary_key=true) chave_app = models.uuidfield(auto_created=true, default=uuid.uuid4, unique=true) usuario = models.foreignkey(usuario, on_delete=models.cascade) descricao = models.charfield(max_length=100)
the usuario
field foreignkey...
the usuario
model this:
class usuario(abstractbaseuser): """ classe que ira gerir o cliente final, cadastrado via app ou webapp """ user_id = models.uuidfield(auto_created=true, default=uuid.uuid4, unique=true, primary_key=true) nome = models.charfield(max_length=120) celular = models.charfield(max_length=20, unique=false) email = models.emailfield(max_length=100, unique=true) ativo = models.booleanfield(default=true) chave_api = models.uuidfield(auto_created=true, default=uuid.uuid4, unique=true) created_at = models.datetimefield(auto_now_add=true) objects = accountmanager() username_field = 'chave_api' required_fields = ['email', 'nome', 'celular'] class meta: ordering = ('-created_at',)
all works nice, when try list apps on django-rest-framework list. did same way on anothers models, no foreign key, , listing ok. error giving on view foreign key.
typeerror @ /apps/ str returned non-string (type uuid)
at database, fields inserted values:
[ { "app_code": "423676e9968d41beaeeeb2da43fc56d8", "chave_app": "86c18ae7809e4bda88830090f309a00c", "descricao": "matrix cashback 2.0", "usuario_id": "998339b46bde40bca053978670626a6f" } ]
i'm using django-rest-framework , django in personal project.
don't know now, did try lots of variants. not recognize foreign key uuid.
my serializer view this:
class appserializer(serializers.serializer): app_code = serializers.uuidfield(read_only=true) usuario = serializers.uuidfield(read_only=true) descricao = serializers.charfield(required=true, max_length=100) chave_app = serializers.uuidfield(read_only=true) chave_api = serializers.uuidfield(required=true) class meta: model = apps fields = ('app_code', 'chave_app', 'usuario', 'descricao') def create(self, validated_data): usuario = usuario.objects.get(chave_api=validated_data.get('chave_api')) return apps.objects.create( descricao=validated_data.get('descricao'), usuario=usuario ) def update(self, instance, validated_data): instance.descricao = validated_data.get('descricao', instance.descricao) instance.save() return instance
some welcome! thanks.
this error on browser when try "list" apps.
founded problem... when try list serializer, used uuidfield
on usuario. need readonlyfield
my correct appserializer
was:
class appserializer(serializers.serializer): app_code = serializers.uuidfield(read_only=true) usuario = serializers.readonlyfield(source='usuario.user_id') descricao = serializers.charfield(required=true, max_length=100) chave_app = serializers.uuidfield(read_only=true) # chave_api = serializers.uuidfield(required=true) class meta: model = apps fields = ('app_code', 'chave_app', 'usuario', 'descricao') def create(self, validated_data): usuario = usuario.objects.get(chave_api=validated_data.get('chave_api')) return apps.objects.create( descricao=validated_data.get('descricao'), usuario=usuario ) def update(self, instance, validated_data): instance.descricao = validated_data.get('descricao', instance.descricao) instance.save() return instance
Comments
Post a Comment