python - 如何解决“元组”对象没有属性“_committed”错误
问题描述
我对 Django rest API 开发比较陌生。我目前正在保存Organisation class
并在更新该模型时遇到以下错误:
错误:“元组”对象没有属性“_committed ”
PUT
只是为了让大家知道:只为调用而不是为调用遇到错误POST
。
模型.py
"""Database model for users in the system"""
org_id = models.AutoField(unique=True, primary_key=True)
org_code = models.CharField(max_length=19, blank=False)
org_name = models.CharField(max_length=100, blank=False, unique=True)
org_shortname = models.CharField(max_length=25, blank=False)
org_ml = models.ForeignKey(Translations, related_name='org_ml', on_delete=django.db.models.deletion.CASCADE,
null=True)
org_type = models.IntegerField()
org_logo = models.ImageField(null=True)
org_address = models.CharField(blank=True, max_length=250)
contact_name = models.CharField(blank=True, max_length=100)
contact_phone = models.CharField(blank=True, max_length=20)
country_code = models.CharField(blank=True, max_length=4)
contact_email = models.EmailField(blank=True, max_length=100)
creation_time = models.DateTimeField(auto_now_add=True, editable=False)
creation_by = models.ForeignKey("User", on_delete=django.db.models.deletion.CASCADE, null=True,
related_name="organisation_creation_by")
modification_time = models.DateTimeField(auto_now=True)
modification_by = models.ForeignKey("User", on_delete=django.db.models.deletion.CASCADE, null=True,
related_name="organisation_modification_by")
status = models.IntegerField(default=0)
def __str__(self):
return str(self.org_id) + " " + self.org_name
class Meta:
db_table = "organisation"
视图.py
try:
req_data = request.body.decode('utf-8')
req_data = json.loads(req_data)
req_org_id = req_data['org_id']
req_org_name = req_data['org_name']
req_org_code = req_data['org_code']
req_org_shortname = req_data['org_shortname']
if "org_ml" in req_data:
req_org_ml = req_data['org_ml']
else:
req_org_ml = None
if "org_type" in req_data:
req_org_type = req_data['org_type']
else:
req_org_type = 0
req_org_logo = req_data['org_logo']
req_org_address = req_data['org_address']
req_contact_name = req_data['contact_name']
req_contact_phone = req_data['contact_phone']
req_country_code = req_data['country_code']
req_contact_email = req_data['contact_email']
if 'status' in req_data:
req_status = req_data['status']
else:
req_status = 0
if req_org_ml:
if req_org_ml['ml_id'] != 0:
trans = Translations.objects.filter(pk=req_org_ml['ml_id']).first()
if trans:
trans.ml_text = req_org_ml['ml_text']
trans.ml_group = req_org_ml['ml_group']
trans.status = req_org_ml['status']
if "details" in req_org_ml:
for item in req_org_ml['ml_details']:
if "sl_id" == 0:
details = Translation_details.objects.create(
ml_id=trans,
language=Language.objects.filter(pk=item['lang_id']),
sl_text=item['sl_text'],
sl_state=item['sl_state'],
status=item['status']
)
details.save()
else:
details = Translation_details.objects.filter(pk=item['sl_id']).first()
details.ml_id = trans,
details.language = Language.objects.filter(pk=item['language']),
details.sl_text = item['sl_text'],
details.sl_state = item['sl_state'],
details.status = item['status']
details.save()
else:
Response({"message": "Record not found"}, status=status.HTTP_400_BAD_REQUEST)
else:
trans = Translations.objects.create(
ml_text=req_org_ml['ml_text'],
ml_group=req_org_ml['ml_group'],
status=req_org_ml['status']
)
trans.save()
for item in req_org_ml['ml_details']:
details = Translation_details.objects.create(
ml=trans,
language=Language.objects.filter(pk=item['lang_id']).first(),
sl_text=item['sl_text'],
sl_state=item['sl_state'],
status=item['status']
)
details.save()
else:
trans = ''
org = Organisation.objects.get(pk=req_org_id)
org.org_name = req_org_name,
org.org_shortname = req_org_shortname,
if trans:
org.org_ml = trans,
org.org_code = req_org_code,
org.org_type = req_org_type,
org.org_logo = None,
org.org_address = req_org_address,
org.contact_name = req_contact_name,
org.contact_phone = req_contact_phone,
org.country_code = req_country_code,
org.contact_email = req_contact_email,
org.status = req_status
org.save()
return Response({"message": "Record updated successfully."}, status=status.HTTP_200_OK)
except Exception as e:
trace_back = traceback.format_exc()
logging.log.error(str(type(e)) + " " + str(trace_back))
return Response({"status": "exception", "message": "Internal Server Error"},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
追溯:
Traceback (most recent call last):
File "D:\Workspace\Cormatix\backend\api\organisation\views.py", line 250, in put
org.save()
File "D:\Workspace\Cormatix\backend\venv\lib\site-packages\django\db\models\base.py", line 746, in save
force_update=force_update, update_fields=update_fields)
File "D:\Workspace\Cormatix\backend\venv\lib\site-packages\django\db\models\base.py", line 784, in save_base
force_update, using, update_fields,
File "D:\Workspace\Cormatix\backend\venv\lib\site-packages\django\db\models\base.py", line 862, in _save_table
for f in non_pks]
File "D:\Workspace\Cormatix\backend\venv\lib\site-packages\django\db\models\base.py", line 862, in <listcomp>
for f in non_pks]
File "D:\Workspace\Cormatix\backend\venv\lib\site-packages\django\db\models\fields\files.py", line 286, in pre_save
if file and not file._committed:
AttributeError: 'tuple' object has no attribute '_committed'
上面还有一个示例代码,只是为了习惯 Django 框架。
解决方案
在您的代码中:
org = Organisation.objects.get(pk=req_org_id)
org.org_name = req_org_name,
org.org_shortname = req_org_shortname,
if trans:
org.org_ml = trans,
org.org_code = req_org_code,
org.org_type = req_org_type,
org.org_logo = None,
org.org_address = req_org_address,
org.contact_name = req_contact_name,
org.contact_phone = req_contact_phone,
org.country_code = req_country_code,
org.contact_email = req_contact_email,
org.status = req_status
每行后面都有逗号(第一行和最后一行除外);可能是另一种语言的习惯,例如 SQL(或者您在括号内思考,将这些视为例如关键字值参数)。这些逗号使值成为元组。删除逗号。
澄清一下:元组不是由值周围的括号定义,而是由(尾随)逗号定义:
tuple1 = 1, 2
tuple2 = (1,)
tuple3 = 1,
integer = (1)
empty = () # empty tuple
推荐阅读
- java - HTML 页面在 JEditorPane 中未正确显示
- python - 洁净室类型数据
- html - 如何使用 CSS / Bootstrap 3 制作斜线
- apache - 如果查询字符串包含关键字,htaccess 发送 404
- javascript - 可汗学院:如果语句 2
- javascript - ReactJS:如何使用 refs() 在组件内设置“innerHTML”?
- scala - 在 Scala 中更新环境
- java - 为什么尝试访问 Azure Datalake 时 Java Azure Function App 冻结?
- android - 如何在低于 30 的 Android sdk 版本中删除图像文件
- reactjs - Auth0 对令牌请求返回 401。Auth0 日志显示登录成功