9.3.2 Creating a View to Modify NewsLink
Objects
In Chapter 3: Programming Django Models and Creating a SQLite Database, I mentioned that we would regret not adding a slug
to our NewsLink
model class. That time is now.
When we built the model, we (pretended that we) had only considered the fact that we were displaying news on the startup’s page, meaning we didn’t need a slug for the URL. The problem we are faced with now is that we need to identify NewsLink
objects in the URL, and we don’t have a good way to do it.
The purpose of this exercise is to provide an example of using the primary key of an object to identify it. The URL pattern and view we use makes use of the id
column in the database, aliased as pk
in Django’s object-relational mapping ...
Get Django Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.