1

I have problem with objects in Django.
My project using multiply databases. When I create object using

obj, _created = self.model.objects.using('default').get_or_create(pk=1)

obj don't have pk but have id

ipdb> obj.pk ipdb> obj.id 1
Why this happened?

  • Take a look to this http://stackoverflow.com/questions/22345711/whats-the-difference-between-model-id-and-model-pk-in-django and the links there – trinchet Apr 06 '16 at 13:07

1 Answers1

0

First look this answers: What's the difference between Model.id and Model.pk in django? Django queries - id vs pk

And the doc of 'get_or_create': https://docs.djangoproject.com/en/1.9/ref/models/querysets/#get-or-create

The object has 'pk' when he was recovered from 'bd'. Probably the object was created, check the variable '_created', it specifies whether a new object was created or was recovered from 'bd'.

Community
  • 1
  • 1
Anderson Lima
  • 246
  • 4
  • 8