Wizard
Wizard

Reputation: 22083

`pk` and `id` are not identical when coping instances with inheritance

I am aware that pk is preferable because id is builtins.They are identical.

However, reference to copy instances which use inheritance, it's not complicated to distinguish them:

In the official tutorial doc

#Due to how inheritance works, you have to set both pk and id to None:
django_blog.pk = None
django_blog.id = None
django_blog.save() # django_blog.pk == 4

There, pk is definitely not identical to id

How to understand it?

Upvotes: 0

Views: 43

Answers (1)

Shadow
Shadow

Reputation: 9427

The action of saving gives your model an id. This will populate your id and pk attributes.

The documentation probably could be more complete by stressing that both would be set.

Upvotes: 1

Related Questions