[Answered ]-Django Model.objects.all() and Model.objects.get(pk=?) return two different results

2👍

If you are using Django-nonrel on GAE, make sure that you don’t set primary_key=True for related models because the engine will use the same primary key for both models. For example, in my case, I have Restaurant and Place with OneToOneRelationship. If we use Place as primary key for Restaurant, a restaurant object created from a place object will share the same pk, thus messing up Restaurant.objects.get(pk=).

Dropping the primary key rule fixes my problem:

class Restaurant(models.Model):
    place = models.OneToOneField(Place) # no primary_key=True

I can’t find this information elsewhere, so I’m posting it here. Hope it helps someone else.

👤Lim H.

Leave a comment