Django return error get_or_create: 'tuple' object has no attribute

I am new to django and I try to use the get_or_create model function, but I get an error even if I have an attribute in my model

AttributeError at /professor/adicionar-compromisso 'tuple' object has no attribute 'dias' Request Method: POST Request URL: http://localhost:8000/professor/adicionar-compromisso Django Version: 1.4.1 Exception Type: AttributeError Exception Value: 'tuple' object has no attribute 'dias' Exception Location: c:\htdocs\rpv\GerenDisponibilidade\professor\models.py in inserirCompromisso, line 63 Python Executable: C:\Python27\python.exe Python Version: 2.7.3 Python Path: ['c:\\htdocs\\rpv\\GerenDisponibilidade', 'C:\\Python27\\lib\\site-packages\\distribute-0.6.27-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\pip-1.1-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\sphinx-1.1.3-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\docutils-0.9.1-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\jinja2-2.6-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\pygments-1.5-py2.7.egg', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages', 'C:\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg-info'] Server time: Seg, 3 Set 2012 17:57:17 -0300 

Model

 class DiaSemana(models.Model): DIAS_CHOICES = ( ("Seg", "Segunda-Feira"), ("Ter", "Terรงa-Feira"), ("Qua", "Quarta-Feira"), ("Qui", "Quinta-Feira"), ("Sex", "Sexta-Feira"), ("Sab", "Sรกbado"), ("Dom", "Domingo"), ) dias = models.CharField(max_length=20, choices=DIAS_CHOICES) 

Here I try to search to check if an existing value exists, otherwise create a new one and save

 for diaSemana in diaSemanas: d = DiaSemana.objects.get_or_create(dias=diaSemana) d.dias = diaSemana; d.save() c.save() c.diaSemana.add(d); 

What's wrong?

+9
source share
3 answers

get_or_create does not just return an object:

Returns a tuple (object, created) , where the object is the received or created object, and the created one is a Boolean value indicating whether a new object was created.

In your case, d was assigned this tuple instead of the expected object, so you get an attribute error. You can fix your code by changing it to:

 d, created = DiaSemana.objects.get_or_create(dias=diaSemana) 

I do not need the following two lines. The call to get_or_create above provides d.dias=diaSemana , so there is no need to assign it again. You probably don't need to call save either.

 d.dias = diaSemana; d.save() 
+32
source

The documentation clearly says that get_or_create returns a tuple (the object created) - and this is exactly the error you see. https://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create

+2
source

instead of this:

 dias = models.CharField(max_length=20, choices=DIAS_CHOICES) 

do:

 dias = models.CharField(max_length=20, choices=DIAS_CHOICES)[0] 

as @Alasdair said, the first in the tuple is the object

+2
source

Source: https://habr.com/ru/post/924475/


All Articles