On Wed, May 6, 2009 at 11:37 PM, George Song <geo...@damacy.net> wrote:
>
> On 5/6/2009 11:18 PM, Lee Hinde wrote:
>> 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.)
>
> I can't be sure what your exact conditions are, but in my test, the
> result is exactly as I described:
>
> {{{
> from django.db import models
> from django.template.defaultfilters import slugify
>
> class Class(models.Model):
>     name = models.CharField(max_length=50)
>     slug = models.SlugField()
>
>     def save(self, force_insert=False, force_update=False):
>         self.slug = slugify(self.name)
>         count = self.__class__.objects.filter(slug=self.slug).count()
>         if count:
>             self.slug = '%s_%d' %(self.slug, count)
>         super(Class, self).save(force_insert, force_update)
>
>  >>> from yoga.models import Class
>  >>> c1 = Class.objects.create(name='Yoga For Kids')
>  >>> c2 = Class.objects.create(name='Yoga For Kids')
>  >>> c3 = Class.objects.create(name='Yoga For Kids')
>  >>> c1.slug
> u'yoga-for-kids'
>  >>> c2.slug
> u'yoga-for-kids_1'
>  >>> c3.slug
> u'yoga-for-kids_1'
> }}}
>
> --
> George


George, thanks very much for the effort.

I'd appreciate any suggestions on the right way to code this.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to