> I don't have a problem with two or three or ten users entering Harry
> Potter into the database; annoyingly though, all but the first user
> will get an error saying the slug must be unique. Can I define the
> slug as having to be unique for the user?

Looks like you want the "unique_together" Meta option:

http://www.djangoproject.com/documentation/model-api/#unique-together

rather than specifying the slug as unique.

> The other problem is in those cases where there will be a duplicate
> slug. I don't want the users to worry about it, so is there a way to
> automatically 'fix' the slug if it's a duplicate? Say, by adding a
> number to it to make it unique?

This is a bit more work (meaning, Django doesn't do this one for
you with one line of code :)

To prevent nettlesome race conditions, likely the best thing to
do is intercept the POST in a transaction, check to see if the
slug already exists for the given user, and then formulaicly find
the next unused one.  You might do something like

 existing = Foo.objects.filter(user_id=request.user.id)
 # new_slug = the proposed slug from the user
 existing = existing.filter(slug__startswith = new_slug)
 i = 0  # to keep track of the highest index we've come across
 offset = len(new_slug)
 for foo in existing:
   s = foo.slug[offset:] # strip off the matching slug part
   if i == 0 and not s:
     # if we already have an exact match,
     # we need an index if we don't
     # already have one
     i = 1
   elif s.isdigit():
      x = int(s)
      if x > i: i = x + 1
 if i > 0: new_slug = "%s%i % (new_slug, i)

NB:  This code is 100% untested and may only work in my head ;)

This does have odd behavior if they have two slugs like
"elvis2000" and "elvis" and then try to add "elvis" a second
time...it will result in "elvis2001" instead of "elvis1".

Once you've modified it, make sure you redirect to the corrected
slug URL too. :)

-tim






--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to