[ Error with python / django1.7 (Exception Type: AttributeError) ]
I'm learning Python and Django and emerged me the following error that I can not solve :
Exception Type: AttributeError
Exception Value: 'QuerySet' object has no attribute 'apellido'
(models)
class Humano(models.Model):
nombre = models.CharField(max_length=30)
apellido = models.CharField(max_length=30)
def __unicode__(self):
return self.nombre
def apellido(self):
return self.apellido
(forms)
class formulario(forms.Form):
nombre = forms.CharField(max_length=30)
apellido = forms.CharField(max_length=30)
(views)
def comparar(request):
if request.method == 'POST':
form = formulario(request.POST)
if form.is_valid():
objeto = Humano(
nombre = form.cleaned_data['nombre'],
apellido = form.cleaned_data['apellido'],
)
objeto2 = Humano.objects.all()
n = objeto2.apellido() # el error me marca esta linea
if n == objeto.apellido:
z = 'los apellidos son iguales'
else:
z = 'los apellidos son distintos'
return render_to_response('index.html', { 'z':z}, context_instance=RequestContext(request))
else:
form = formulario()
return render_to_response('index.html', {'form':form}, context_instance=RequestContext(request))
The purpose of the view is to compare a apellido received through a form and compare it with other existing in the database .
Answer 1
There are a couple of issues here:
Since objeto2
is a queryset, for you to make the comparison,
objeto = Humano(
nombre = form.cleaned_data['nombre'],
apellido = form.cleaned_data['apellido'],
)
objeto_qs = Humano.objects.all()
#Note that you dont need the call here.. Moreover, unless you have created a new manager named `appelidos`, the () call does not work
for n in objeto_qs:
if n.appellido == objeto.apellido:
z = 'los apellidos son iguales'
else:
z = 'los apellidos son distintos'
Another thing is, you are not saving the created object. This means the object is lost once this function executes. You might want to consider using Humano.objects.create(..)
Finally, you dont have to loop through the objects individually. You can just query the database like this:
if Humano.objects.filter(apellido=objeto.apellido).exists():
z = 'los apellidos son iguales'
else:
z = 'los apellidos son distintos'
Answer 2
Theres a few things here that could be changed.
The first is the actual error where objeto2 = Humano.objects.all()
is returning a queryset and as such it isn't returning a single model
Without knowing spanish, I'm assuming you're trying to figure out if any of the other objects in the database have the same apellido
. To do this you can use filter
with exists
which will tell you if any exist or not.
any_apellido_exist = Humano.objects.filter(apellido=form.cleaned_data['apellido']).exists()
Second, your whole form is duplicating fields and will make it hard to maintain, you might want to consider using a ModelForm
class formulario(forms.ModelForm):
class Meta:
model = Humano
Answer 3
for objeto2 in Humano.objects.all()
n = objeto2.apellido()
if n == objeto.apellido:
z = 'los apellidos son iguales'
else:
z = 'los apellidos son distintos'