django - Error with python / django1.7 (Exception Type: AttributeError) -


i'm learning python , django , emerged me following error 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 view compare apellido received through form , compare other existing in database .

there couple of issues here:

since objeto2 queryset, make comparison,

objeto = humano(     nombre = form.cleaned_data['nombre'],     apellido = form.cleaned_data['apellido'], )  objeto_qs = humano.objects.all() #note dont need call here.. moreover, unless have created new manager named `appelidos`, () call not work n in objeto_qs:     if n.appellido == objeto.apellido:         z = 'los apellidos son iguales'     else:         z = 'los apellidos son distintos' 

another thing is, not saving created object. means object lost once function executes. might want consider using humano.objects.create(..)


finally, dont have loop through objects individually. can query database this:

if humano.objects.filter(apellido=objeto.apellido).exists():     z = 'los apellidos son iguales' else:     z = 'los apellidos son distintos' 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -