我正在使用 Django 和 Django REST 框架编写一个 REST Api。我有一个包含多个属性的配置文件模型,我正在尝试为配置文件编写更新函数。我有两个属性的问题:
attribute1 = models.CharField()
attribute2 = models.IntegerField()
在我的更新函数 (in views.py
) 中,我首先检索将要更新的配置文件:
profile = Profile.objects.get(id=profile_id)
然后我使用 get 函数更新字段(以便不在请求中的每个字段保持不变):
attribute1 = request.DATA.get('attribute1', profile.attribute1)
attribute2 = request.DATA.get('attribute2', profile.attribute2)
我遇到的问题是,当我使用 and 创建配置文件的实例时attribute1 = "Reg"
,attribute2 = 1
但是当我更新模型的另一个字段时,attribute1
变得u\"(u\\'Reg\\',)\"
和attribute2
变得u\"'(1,)'
. 我不知道这个问题是从哪里来的(我对其他领域没有问题)。你有想法吗?