On Wed, May 6, 2009 at 11:15 PM, Lee Hinde <leehi...@gmail.com> wrote: > On Wed, May 6, 2009 at 10:54 PM, George Song <geo...@damacy.net> wrote: >> >> On 5/6/2009 10:34 PM, Lee Hinde wrote: >>> On Wed, May 6, 2009 at 10:22 PM, George Song <geo...@damacy.net> wrote: >>>> On 5/6/2009 9:57 PM, Lee Hinde wrote: >>>>> I have this as part of the model for a class called "Class" >>>>> >>>>> >>>>> def save(self, force_insert=False, force_update=False): >>>>> start = defaultfilters.slugify(self.Name) >>>>> count = Class.objects.filter(Slug__equal=start).count() >>>>> if count != 0: >>>>> filterme = "%s_%d" % (self.Name, count) >>>>> self.Slug = defaultfilters.slugify(filterme) >>>>> else: >>>>> self.Slug = start >>>>> >>>>> >>>>> The reason is, it's ok to have duplicate Names, and I want to use a >>>>> Slug as the canonical url. >>>>> >>>>> I have a bunch of data to import and so , in shell I run >>>>> >>>>> c = Class.objects.all() >>>>> for o in c: >>>>> o.save() >>>>> >>>>> >>>>> My goal would be to see something like: >>>>> >>>>> yoga-for-kids >>>>> yoga-for-kids_1 >>>>> yoga-for-kids_2 >>>>> >>>>> What's weird is I'm getting: >>>>> >>>>> yoga-for-kids >>>>> yoga-for-kids >>>>> yoga-for-kids_2 >>>>> >>>>> i.e., I get dupes and then not. >>>>> hasn't >>>>> I've played with it most of the evening and now I'm hoping someone >>>>> with a fresh pair of eyes might have a suggestion. >>>>> >>>>> Thanks in advance. >>>> I'm not sure why you're getting yoga-for-kids_2, what I expect is >>>> yoga-for-kids, yoga-for-kids_1, then more yoga-for-kids_1 if your slug >>>> field is not unique=True. >>>> >>>> In any case, your logic will keep generating yoga-for-kids_1 because >>>> there will only ever be one count for yoga-for-kids in your DB. >>>> >>> >>> Well, depends on when it's run, right? yoga-for-kids_1 would be the >>> correct response for the 2nd record. >>> >>> In this case I'm updating imported data, so there's a pre-existing >>> match. If it's a new record, it should come back zero, sine the record >>> hasn't been saved yet? >>> >>> Thanks for looking at it. >> >> Well, I imagine your Class.Name for your test case is "Yoga For Kids", >> right? >> >> So your `start` is always going to be "yoga-for-kids" no matter what. >> >> Since you're search on that slug, you should only ever have one record >> since your slugs are supposed to be unique. So your re-generated slug >> will always be "yoga-for-kids_1". >> >> -- >> George >> > > > Yes, I see what you're saying and it means I've left out a step. > Before I run the model.save() loop, I clear the existing slug data, so > it starts clean each run. >
But still should always leave me with 1... So, how did I end up with 2 blanks and one 2? Assuming I start off with blanks. First time - no match, save as is Second time - one match save as X_1 third time, still one match (this is what you described), should save again as x_1 (I'd tried 'startedwith' but that had other problems.) --~--~---------~--~----~------------~-------~--~----~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---