URLs: mymodel_id vs object_id vs pk ....

65 views
Skip to first unread message

Thomas Güttler

unread,
Jun 4, 2014, 9:07:52 AM6/4/14
to django...@googlegroups.com
What is the best practice for named URL parameter which refer to a primary key of a model?

I have seen many ways:

- object_id
- mymodel_id
- pk
- id
- .....

example:

urlpatterns = [
url(r'^article/(?P<WHAT_NAME_HERE>\d+)/$', 'article.view'),
...
]

Up to now we used "mymodel_id" but it gets boring to type in "mymodel".

This is a question about best practice:

Which do you use (and why)?


Related: https://docs.djangoproject.com/en/dev/topics/http/urls/#named-groups

Thomas

Juergen Schackmann

unread,
Jun 4, 2014, 9:25:45 AM6/4/14
to django...@googlegroups.com
I am not aware of any best practices for wording here, though my general recommendaton would the wording should be identical to the attribute name of the model, e.d. 'id', 'slug' or whatever. I would not add the "mymodel" prefix as long as it becomes clear from the context to which model it belongs to. 

Ilya Kazakevich

unread,
Jun 4, 2014, 9:28:27 AM6/4/14
to django...@googlegroups.com
If you use "DetailView"
(https://docs.djangoproject.com/en/dev/ref/class-based-views/generic-display
/#django.views.generic.detail.DetailView) you may call this field "slug" (it
is default). But you can overwrite it.

But in your case custom function is used, so you may call this field
whatever you want.
I believe "id" is nice.

I use DetailView unless I need really custom logic, so I use "slug" almost
always.

Ilya Kazakevich,
JetBrains PyCharm (Best Python/Django IDE)
http://www.jetbrains.com/pycharm/
"Develop with pleasure!"
>--
>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 http://groups.google.com/group/django-users.
>To view this discussion on the web visit
>https://groups.google.com/d/msgid/django-users/538F19FF.1090209%40tbz-pari
>v.de.
>For more options, visit https://groups.google.com/d/optout.

monoBOT

unread,
Jun 4, 2014, 9:57:41 AM6/4/14
to django...@googlegroups.com
Thats a matter of taste ... i use model_id




  Thomas

--
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+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/538F19FF.1090209%40tbz-pariv.de.
For more options, visit https://groups.google.com/d/optout.



--
monoBOT
Visite mi sitio(Visit my site): monobotblog.alvarezalonso.es

Sergiy Khohlov

unread,
Jun 4, 2014, 12:38:34 PM6/4/14
to django-users
Jnt of the best way is a slug. 
 I nether do use id or object_id by security reason (hacker can try to access another object or  count on objects in your application, such as administrator usually  has id as 0 or small number. It is good target for attack). Keep in mind that id is mutable after moving to the new database (ex: you can not use direct link to the product in your webshop). 

Many thanks,

Serge


+380 636150445
skype: skhohlov


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 http://groups.google.com/group/django-users.

Thomas Güttler

unread,
Jun 5, 2014, 5:42:12 AM6/5/14
to django...@googlegroups.com
Thank you for answering my question.

Unfortunately "slug" does not work in my context.

My model does not have a "slug". It is like a ticket/issue system. There
are several hundred of tickets each day.

Thomas Güttler
Reply all
Reply to author
Forward
0 new messages