django: How do I hash a URL from the database object's primary key?


I'm trying to generate URLs for my database objects. <a href="http://agiliq.com/books/djangodesignpatterns/misc.html#do-not-use-primary-keys-in-urls" rel="nofollow">I've read</a> I should not use the primary key for URLs, and a stub is not a good option for this particular model. Based on the advice in that link, I played around with zlib.crc32() in a Python interpreter and found that values often return negative numbers which I don't want in my URLs. Is there a better hash I should be using to generate my URLs?

<strong>UPDATE:</strong> I ended up using the bitwise XOR masking method suggested by David below, and it works wonderfully. Thanks to everyone for your input.


First, "don't use primary keys in URLs" is only a very weak guideline. <em>If</em> you are using incremental integer IDs <em>and</em> you don't want to reveal those numbers, then you could obfuscate them a little bit. For example, you could use: masked_id = entity.id ^ 0xABCDEFAB and unmasked_id = masked_id ^ 0xABCDEFAB.

Second, the article you linked to is <em>highly</em> suspicious. I would not trust it. First, CRC32 is a one-way hashing function: it's impossible (in general) to take a CRC32 hash and get back the string used to create that hash. You'll notice that he doesn't show you how to look up a Customer given the CRC32 of their pk. Second, the code in the article doesn't even make sense. The zlib.crc32 function expects a byte string, while Customer.id will be an integer.

Third, be careful if you want to use a slug for a URL: if the slug changes, your URLs will also change. This may be okay, but it's something you'll need to consider.


  • ASP.NET MVC: Unit Tests Error - “The type X exists in both Y and Y”
  • UITableView is under the TabBar
  • styling with jQuery
  • Eclipse as IDE + Mercurial for version control + ? Bug tracking = Good idea?
  • Difference between return View() and return View(new Model())
  • How to check Url Image is exist or not in php [duplicate]
  • display all elements in a nested cell array (with character entries)
  • WooCommerce: Change priority of tabs by number of product ratings
  • Write TestCases using PageFactory (Selenium-WebDriver). Advantages? [closed]
  • perl - Math with duplicate entries in array
  • Child object property change fires parents DependencyPropertyChanged callback?
  • got “libatk-1.0.so.0: undefined symbol” when I tried ipython --pylab in Ubuntu 13.10
  • Allow QGraphicsView to move outside scene
  • What is a more elegant way of dealing with multiple or's in an if statment
  • How to warn user not to leave the page? [duplicate]
  • Getting SPI temperature data from outside of class
  • Retrieve 3rd MAX salary in Hive
  • View.layout() works until next UI update
  • MySQL ordering a date range to the beginning, then ordering everything else by other criteria
  • What is the diff. between default.properties and project.properties?
  • Git for windows has stopped working
  • Tensorflow crash using tf.train.Saver() with GPU
  • ASP.NET GridView throws: The version of SQL Server in use does not support datatype 'date'
  • Bundling python(“.py”)files along with java class files for a web application
  • How does the dispatcher work when mixing sync/async with serial/concurrent queue?
  • Facebook Error (#200) The user hasn't authorized the application to perform this action (PHP)
  • Ruby on Rails: Get mediaplayer information (iTunes, TRAKTOR, Cog; current song + playlist)
  • Disable account chooser FirebaseUI React
  • Problems to understand DXGI DirectX 11 Desktop Duplication to get a Buffer or Array
  • C++ STL stack pop operation giving segmentation fault
  • VSTS work items list through REST API
  • Why my AngularJS async test in Jasmine 1.3.x is not working?
  • Can someone explain this Java code (formatting the output using System.out.format) to me?
  • Cross compile glibc for arm, got undefined reference to some unwind functions
  • multiple button click in asp.net MVC 3
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • JavaScript RegExp Replace