Search code examples
pythondjangotraceback

Django python error when list object


I have a class Patient in models.py with

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

When I attribute a variable to all objects or filter some objects using, for example,

patient = Patient.objects.all()

Ok, no message errors.

But when I try list this objects, I receive the following message error

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

Edit:

That code occurs when I digit in shell

>>> patient

to list objects that I've created

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

Solution

  • __unicode__() should return a string, not tuple:

    def __unicode__(self):  
        return self.name_txt