Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am new to django and I am trying to use get_or_create model function but I get an error even I have the 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 am trying to search to check if there is existing value, otherwise create new 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?

like image 928
dextervip Avatar asked Sep 03 '12 21:09

dextervip


2 Answers

get_or_create does not just return the object:

Returns a tuple of (object, created), where object is the retrieved or created object and created is a boolean specifying whether a new object was created.

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

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

The following two lines look unnecessary to me. The get_or_create call above ensures that d.dias=diaSemana, so there's no need to assign it again. There's probably no need to call save either.

d.dias = diaSemana;
d.save()
like image 95
Alasdair Avatar answered Oct 23 '22 04:10

Alasdair


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 one in the tuple is the object

like image 3
elad silver Avatar answered Oct 23 '22 03:10

elad silver