我有这个当前的模型:
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)
usuario
字段是ForeignKey ...
usuario
模型是这样的:
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',)
一切都很好,但是当我尝试在django-rest-framework上列出应用程序作为列表时。我在其他模型上做了同样的事情,没有外键,列表也没关系。错误仅在此视图上使用外键。
/ apps /的TypeError str 返回非字符串(类型UUID)
在数据库中,我的字段插入了以下值:
[
{
"app_code": "423676e9968d41beaeeeb2da43fc56d8",
"chave_app": "86c18ae7809e4bda88830090f309a00c",
"descricao": "Matrix Cashback 2.0",
"usuario_id": "998339b46bde40bca053978670626a6f"
}
]
我在个人项目中使用Django-rest-framework和Django。
不知道现在该做什么,尝试了很多变种。它只是不将外键识别为UUID。
我的Serializer View是这样的:
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
欢迎一些帮助!感谢。
当尝试" list"时,这是浏览器上的错误所有应用。
答案 0 :(得分:1)
成立问题......
当我尝试列出序列化程序时,我在usuario上使用了ReadOnlyField
。但需要AppSerializer
我的正确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
现在是:
douces.xxx