gpt4 book ai didi

python - 列表对象时出现 Django python 错误

转载 作者:太空宇宙 更新时间:2023-11-04 09:01:38 26 4
gpt4 key购买 nike

我在 models.py 中有一个 Patient 类

class Patient(models.Model):
cpf_id = models.CharField(null=True, blank=True, max_length=15, unique=True, validators=[validate_cpf])
rg_id = models.CharField(max_length=15, null=True, blank=True)
name_txt = models.CharField(max_length=50)
number_record = models.AutoField(primary_key=True)
medical_record_number = models.CharField(max_length=25, null=True, blank=True)
natural_of_txt = models.CharField(max_length=50, null=True, blank=True)
citizenship_txt = models.CharField(max_length=50, null=True, blank=True)
street_txt = models.CharField(max_length=50, null=True, blank=True)

class Meta:
permissions = (
("view_patient", "Can view patient"),
)

def __unicode__(self): # Python 3: def __str__(self):
return \
self.name_txt, self.cpf_id, self.rg_id, self.medical_record_number, self.natural_of_txt, \
self.citizenship_txt, self.street_txt

当我将变量归于所有对象或使用例如过滤某些对象时

patient = Patient.objects.all()

好的,没有消息错误。

但是当我尝试列出这个对象时,我收到以下消息错误

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 74, in __repr__
return repr(data)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 423, in __repr__
u = six.text_type(self)
TypeError: coercing to Unicode: need string or buffer, tuple found

编辑:

当我在 shell 中输入数字时出现该代码

>>> patient

列出我创建的对象

>>>> patient = Patient.objects.all()

最佳答案

__unicode__()应该返回一个字符串,而不是元组:

def __unicode__(self):  
return self.name_txt

关于python - 列表对象时出现 Django python 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24759183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com