1

I have a queryset like this:

asignaturas_equivalencia_alumno = list(Asignaturas_Equivalencia.objects.filter(matricula=inscripcion.usuario.adicional.matricula, origen_de_equivalencia = "SEP").distinct())

then I loop over them and I print the index, if this is in the list and a property of the model:

for _as in asignaturas_equivalencia_alumno:
    print _as in asignaturas_equivalencia_alumno, asignaturas_equivalencia_alumno.index(_as), _as.clave_materia

and the result is different but the index, the index always returns 0, the output:

True 0 L1C115
True 0 L1PS101
True 0 L1C116
True 0 L1C118
True 0 L1PS105
True 0 L1PS107
True 0 L1PS109
True 0 L1PS111
True 0 L1C113
True 0 L1C114
True 0 L1C117
True 0 L1PS102

Thank you :)

EDIT

The model:

class Asignaturas_Equivalencia(models.Model):
    matricula = models.CharField(max_length=10, primary_key=True)
    clave_materia = models.CharField(max_length=9)
    calificacion = models.CharField(max_length=2)
    origen_de_equivalencia = models.CharField(max_length=4)
    fecha_registro = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'diccionario_equivalencias_materias'

EDIT AGAIN

The only solve for this question is to use enumerate.

Victor Castillo Torres
  • 10,581
  • 7
  • 40
  • 50

3 Answers3

1

You have different items, as is shown by _as.clave_materia. I wonder what this class definition looks like.

Maybe you wrote your own __eq__ method, or similar. If you did, it might be buggy and always return True for the first element of your list of objects. Does it sound like an explanation?

Something like this (IPython session):

In [44]: class Foo:   
    def __eq__(self, s): return True
   ....:     

In [45]: a = Foo()

In [46]: b = Foo()

In [47]: a == b
Out[47]: True

In [48]: l = [a,b, Foo()]

In [49]: l
Out[49]: 
[<__main__.Foo at 0xb5c68b2c>,
 <__main__.Foo at 0xb670ad0c>,
 <__main__.Foo at 0xb5e0352c>]

In [50]: for item in l:
    print(l.index(item))
   ....:     
0
0
0
André Laszlo
  • 15,169
  • 3
  • 63
  • 81
1

Check and double-check your primary keys. It seems to me that they are the only things that could mess up your equality checks like this.

The __eq__ method of Django's ORM returns True when two objects are of the same class and have the same pk. I'm guessing it gets confused when you have two fields with primary_key = True. It should throw some error afaik, but maybe it just leads to undefined behavior.

From the Django Models documentation:

"Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added)."

(Emphasis mine)

André Laszlo
  • 15,169
  • 3
  • 63
  • 81
  • I already removed one primari_key and the index is always 0 :C – Victor Castillo Torres Jan 10 '14 at 19:27
  • Well, then I'm out of guesses. Unless, somehow, all `_as.matricula` (whatever it is) are equal. Can you add it to your printout as well, since it is the pk? – André Laszlo Jan 10 '14 at 19:45
  • Well I think is my problem because, any field in that model is repeated so no one can not be primari_key, and as the table was already done, I did not I only did the model to use it with django I had to put at least a primari_key – Victor Castillo Torres Jan 10 '14 at 20:00
  • If you don't set a primary key explicitly (using `primary_key = True`), Django will create one for you (and it will be unique). Cheers. – André Laszlo Jan 10 '14 at 20:03
  • Rail's Active Records take care of schema migrations quite nicely. Django seems to be going in the same direction. Maybe you're interested in [Simple Migrations](https://github.com/ricardochimal/simplemigrations/tree/master) and [Migratory](https://bitbucket.org/DeadWisdom/migratory/wiki/Home). If you're already running >=1.7 you should definitely check out [Django's schema migration commands `migrate` and `makemigrations`](https://docs.djangoproject.com/en/dev/topics/migrations/) – André Laszlo Jan 10 '14 at 20:09
  • I'm working with Django 1.3 and I use South, believe me if it was for me I had added an id field to the table, but i can not modified it, because I'm not the only one who uses it at my work – Victor Castillo Torres Jan 10 '14 at 20:11
1

As others have rightfully mentioned the equality test is probably fragile for the ordered iterable members. But if you want to loop over contents and have indices then it is easier and more efficient to simply use enumerate.

for index,_as in enumerate(asignaturas_equivalencia_alumno):
     print _as in asignaturas_equivalencia_alumno, index, _as.clave_materia