Hi Anssi, thanks for your answer.
I already thought about that, but checking the code from
django.db.models.base.Model#__eq__() is not telling me the same:
def __eq__(self, other):
return isinstance(other, self.__class__) and self._get_pk_val()
== other._get_pk_val()
On 03/22/2013 05:02 PM, akaariai wrote:
> On 22 maalis, 15:44, Roberto López López <[email protected]> wrote:
>> Hi,
>>
>> I have a problem with my data model while doing field lookups. This is
>> my models.py:
>>
>> from django.db import models, IntegrityError
>>
>> # Create your models here.
>>
>> class Model1(models.Model):
>> title = models.CharField(max_length=15)
>> models2 = models.ManyToManyField('Model2', through='ThroughModel')
>>
>> def __unicode__(self):
>> return self.title
>>
>> class Model2(models.Model):
>> title = models.CharField(max_length=15)
>>
>> def __unicode__(self):
>> return self.title
>>
>> class ThroughModel(models.Model):
>> model1 = models.ForeignKey(Model1)
>> model2 = models.ForeignKey(Model2)
>> lead = models.BooleanField(default=False)
>>
>> def __unicode__(self):
>> return u'{0} - {1} - {2}'.format(self.model1, self.model2,
>> self.lead)
>>
>> Testing it on the django shell:
>>
>>>>> m1 = Model1.objects.create(title='blabla')
>>>>> m2 = Model2.objects.create(title='blabla2')
>>>>> m1.__eq__(m2)
>> False # OBVIOUSLY>>> t =
>> ThroughModel.objects.create(model1=m1, model2=m2)
>>>>> ThroughModel.objects.filter(model1__exact=m1)
>> [<ThroughModel: blabla - blabla2 - False>] # OK>>>
>> ThroughModel.objects.filter(model1__exact=m2)
>>
>> [<ThroughModel: blabla - blabla2 - False>] # NOT OK!!!
>>
>> Am I missing anything? Can anyone spot the problem?
>>
>> Thanks for your advice.
>>
>> Regards,
>>
>> Roberto
> The problem here is that Django doesn't do any verification that the
> given model values for lookups are of correct type. My bet is that
> m1.pk == m2.pk. So, Django will create a query using m2.pk in
> model1__exact=m2, and this is the reason you get the results you get.
>
> - Anssi
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.