difference between id and pk

685 views
Skip to first unread message

amit pant

unread,
Nov 2, 2018, 7:37:11 AM11/2/18
to Django users
I want to know, what is main difference between pk and id on django.
Would appreciate if any one could help me.
Thanks

Christian Ledermann

unread,
Nov 2, 2018, 8:39:29 AM11/2/18
to django...@googlegroups.com
If you do not add a primarykey explicitly django will create a field `id` for you by default.
Normally this is an autoincrement integer.

You may name your primary key field to whatever you want.

`pk` is an alias that will always be the field you defined as your primary key field.


Normally you will be fine with the autocreated primary key fields in which case `id` and `pk`
will have the same effect, but when you choose to give your field a different name `id` and `pk` 
will differ.

In your code you should prefer `pk` over `id` for the above reasons


--
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 django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d8f5be26-2319-4a81-82d0-97c5ddfcb048%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Best Regards,

Christian Ledermann

Newark-on-Trent - UK
Mobile : +44 7474997517

https://uk.linkedin.com/in/christianledermann
https://github.com/cleder/


<*)))>{

If you save the living environment, the biodiversity that we have left,
you will also automatically save the physical environment, too. But If
you only save the physical environment, you will ultimately lose both.

1) Don’t drive species to extinction

2) Don’t destroy a habitat that species rely on.

3) Don’t change the climate in ways that will result in the above.

}<(((*>
Reply all
Reply to author
Forward
0 new messages