views:

264

answers:

3

What would be the generic way to create record title and slub based on the ID? I am working with django-photologue here. I want to save a record with title and slug based on the PK. The generic problem is that I can't get the PK until the record is saved into database. On the other side, I can't save it without title and slug.

What common solution is that that kind of problem?

+1  A: 

Normally you don't use the primary key at all. If your concern is just to automatically generate unique slugs (which is the only reason I can see to do what you're trying to do), then you want an AutoSlugField, which creates a unique slug by increasing an appended number on the slug until it is unique.

There's an implementation of AutoSlugField which is part of django-command-extensions.

Dominic Rodger
That is one thing. I would like to name the file based on the record ID. I would like to have that kind of relationship for easier management. What is the other solution to generate unique filenames?
mdrozdziel
A: 

To name the file based on record ID you have several options:

a) Try to predict ID:

max_pk = self.__class__.objects.aggregate(max_pk=Max('pk'))['max_pk'] or 0
predicted_id = max_pk+1

b) Rename file in post_save when ID is known.

You can also use md5 hash or random strings for generating unique file names.

Btw, there is separate django-autoslug app.

Mike Korobov
(a) is probably not a good idea if your app's going to have any degree of concurrent writes. (b) is a neat idea.
Dominic Rodger
I agree that (a) is not that great. In geeral it may be considered as a quick hack. But if it is used as a part of upload_to function for FileField then generated duplicate filenames will be auto-appended with '_' by django so there won't be name collisions. If you access files in common way - using path stored in FileField - then there is no problems. But agree, the mapping pk <-> file name may be broken in this case.
Mike Korobov
+1  A: 

If you URIs are supposed to look like "example.com/${obj.id}-${sluggify( obj.title )}" then generate these uri when you use them. This uri contains no data that's not in the DB already, so don't add it again. The slug's sole purpose is making url's look nicer to people and search engines.

Take Stackoverflow as an example: http://stackoverflow.com/questions/1537149/slugs-have-no-meaning-and-can-be-anything

If you want to select by the slug only, it has to be a Primary Key, unique and immutable. You should be aware that having another PK, the usual id column, would be unneeded.

I don't say slugs are bad, nor that saving slugs is always bad. There are many valid reasons to save them in the database, but then you need to think about what you're doing.

Selecting data by their PK (and ignoring the slug) on the other hand requires no thinking, so that should be the default way to go.

THC4k
+1 I have never thought about slugs in this way.
Luiz Damim
Your post just says "slugs are bad", when actually (IMHO at least) slugs have real value. What if you don't want to expose your database IDs? What if you think (as I do) that URLs look a lot nicer as `http://example.com/entries/foo` than `http://example.com/entries/1238/foo`? At that point, storing the slugs in the database has value. They don't denormalise the database, since they are generally only tied to the *initial* title, and don't actually have to be tied to anything.
Dominic Rodger
Absolutely disagree with THC4k and agree with Dominic. Slugs *do* have real value, and it is pretty nasty to do what Stackoverflow does and use both the (effectively random) slug and the ID in the URL. IMO, slugs should be the canonical way to link to a page, and should therefore not be generated dynamically.
Daniel Roseman
Well one thing is that stackoverflow way of handling slugs is really bad for SEO/SEM.
mdrozdziel
@Acidburn2k - I don't think it is, provided they specify which is the canonical URL. You'll find SO pages turn up very highly ranked on Google, for example.
Dominic Rodger
Well, one thing is that most links present on the web are canonical. But there is no point into allowing people to use custom slugs which point to the same content.
mdrozdziel