Re: overriding save-method

2010-10-30 Thread skyjur
Write a custom model field:
http://docs.djangoproject.com/en/dev/howto/custom-model-fields/

Or even better, take already written one:
http://djangosnippets.org/snippets/377/

On Oct 27, 9:14 am, Patrick  wrote:
> hi,
>
> i am trying to override the save-method of one of my models. i want to
> save the json-representation of any object in a text field. somehow it
> doesn't seem to work.
>
> class Setting(models.Model):
>     name = models.CharField(max_length=100)
>     value = models.TextField()
>
>     def save(self, *args, **kwargs):
>         self.value = json.dumps(self.value)
>         super(Setting, self).save(*args, **kwargs)
>
> any ideas?! thank you..

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



Re: overriding save-method

2010-10-28 Thread Patrick
that's how i did it finally... i had to refresh my knowledge about the
property statement first :) thanks again!

class Setting(models.Model):
name = models.CharField(max_length=100)
_value_json = models.TextField()
def _set_json_value(self, value):
self._value_json = json.dumps(value)
def _get_json_value(self):
return json.loads(self._value_json)
value = property(_get_json_value, _set_json_value)

On 27 Okt., 23:40, Patrick  wrote:
> hm .. i thought of that too but i considered it not to be the best
> approach.
> i will try that snippet though.. thank you!
>
> On 27 Okt., 15:49, Shawn Milochik  wrote:
>
> > Try this instead:
>
> >http://djangosnippets.org/snippets/1478/
>
> > Or you could do it manually in your model:
>
> > 1. Add field _value_json to your model.
> > 2. Add functions (get_value, set_value) which do the simplejson work.
> > 3. Add a property named 'value' with get_value and set_value as its
> > getter and setter.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



Re: overriding save-method

2010-10-27 Thread Patrick
hm .. i thought of that too but i considered it not to be the best
approach.
i will try that snippet though.. thank you!

On 27 Okt., 15:49, Shawn Milochik  wrote:
> Try this instead:
>
> http://djangosnippets.org/snippets/1478/
>
> Or you could do it manually in your model:
>
> 1. Add field _value_json to your model.
> 2. Add functions (get_value, set_value) which do the simplejson work.
> 3. Add a property named 'value' with get_value and set_value as its
> getter and setter.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



Re: overriding save-method

2010-10-27 Thread Shawn Milochik
Try this instead:

http://djangosnippets.org/snippets/1478/

Or you could do it manually in your model:

1. Add field _value_json to your model.
2. Add functions (get_value, set_value) which do the simplejson work.
3. Add a property named 'value' with get_value and set_value as its
getter and setter.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



overriding save-method

2010-10-27 Thread Patrick
hi,

i am trying to override the save-method of one of my models. i want to
save the json-representation of any object in a text field. somehow it
doesn't seem to work.

class Setting(models.Model):
name = models.CharField(max_length=100)
value = models.TextField()

def save(self, *args, **kwargs):
self.value = json.dumps(self.value)
super(Setting, self).save(*args, **kwargs)

any ideas?! thank you..

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



Model super class with overriding save method

2010-05-30 Thread Amit Prahesh
Hi,

because of hysterical raisins I need that the models of a simple app I
was tasked upon have their primary fields to consist of strings of
randomly generated characters (think something like
'876nce8yr85yndxw45') of a given length. I'm trying to create a super
class that provides this facility, so that the actual models derive
from it. I came up with

class CustomModel(models.Model):
id = models.CharField(max_length=12 primary_key=True)

class Meta:
abstract = True

def save(self, force_insert=False, force_update=False):
if not self.id:
try:
while True:
id = random_identifier()
self.__class__.objects.get(pk=id)
except self.__class__.DoesNotExist:
self.id = id
models.Model.save(self, force_insert, force_update)

apart for a window of collisions on clusters, is there a better way to
accomplish this? Ideas?

Thanks a lot,
Amit.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



Re: Overriding save() method on models to automatically add ManyToMany relationship

2009-06-09 Thread ringemup


No, you have to save self before you can add categories to it, because
you can't save an M2M if either object is missing a primary key.

In any case, it turns out that the reason is that the admin saves the
submitted M2M data after the save() method is called on the main
model.  So it erases and overwrites anything you've added.  The
solution was to intercept the form data in the ModelAdmin's save_form
() method and to add the default to cleaned_data if the list of
categories is empty.


On Jun 9, 6:30 am, Frédéric Hébert  wrote:
> Hi,
>
> > defsave(self, *args):
> >models.Model(save, *args)
> >category = Category.objects.all()[0]
> >self.categories.add(category)
>
> > This does not work, I'm sure it's saving ManyToMany relationships
> > later on in thesaveprocess.  Is there a way to make this work?
>
> IMHO, it's make sense : you call the parent save method before
> defining new categories on it. How parent save method could know
> anything about these new categories ?
>
> Have you try :
>
> def save(self, *args, **kwargs):
> category = Category.objects.all()[0]
> self.categories.add(category)
> super(Product, self).save(*args, **kwargs)
>
> Frédéric
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Overriding save() method on models to automatically add ManyToMany relationship

2009-06-09 Thread Frédéric Hébert

Hi,
> defsave(self, *args):
>    models.Model(save, *args)
>    category = Category.objects.all()[0]
>    self.categories.add(category)
>
> This does not work, I'm sure it's saving ManyToMany relationships
> later on in thesaveprocess.  Is there a way to make this work?
>


IMHO, it's make sense : you call the parent save method before
defining new categories on it. How parent save method could know
anything about these new categories ?

Have you try :

def save(self, *args, **kwargs):
category = Category.objects.all()[0]
self.categories.add(category)
super(Product, self).save(*args, **kwargs)

Frédéric

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



Re: Overriding save() method on models to automatically add ManyToMany relationship

2009-05-24 Thread ringemup

I'm encountering this same problem.  Did you ever find a solution?

On Apr 27, 1:38 pm, Adam Olsen  wrote:
> On Mon, Apr 27, 2009 at 11:27 AM, Alex Gaynor  wrote:
> > The issues if the method, it's nonsensical and doesn't correspond to
> > anything(you are instantiating models.Model with save as the first
> > argument), in Python the correct way to call the parent class's method is:
>
> > super(MyClass, self).save(*args, **kwargs)
>
> It was an example, complete with a typo.  It should have been:
>
> def save(self, *args):
>   models.Model.save(self, *args)
>   category = Category.objects.all()[0]
>   self.categories.add(category)
>
> However, that point is not the problem.  Even with the proper syntax,
> it doesn't save the ManyToMany relationship.
>
> --
> Adam Olsen
> SendOutCards.comhttp://www.vimtips.orghttp://last.fm/user/synic
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Problem with overriding save() method

2009-05-07 Thread George Song

On 5/7/2009 8:05 AM, Lee Hinde wrote:
> On Wed, May 6, 2009 at 11:37 PM, George Song  wrote:
>> On 5/6/2009 11:18 PM, Lee Hinde wrote:
>>> On Wed, May 6, 2009 at 11:15 PM, Lee Hinde  wrote:
 On Wed, May 6, 2009 at 10:54 PM, George Song  wrote:
> On 5/6/2009 10:34 PM, Lee Hinde wrote:
>> On Wed, May 6, 2009 at 10:22 PM, George Song  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.

I would probably define a utility function that generically accepts a 
list of current slugs and the slug that you want, and returns a unique slug.

In the function body would be whatever logic you want to use to make 
that slug unique, among all the slugs for a specific model.

{{{
def make_unique_slug(all_slugs, slug):
 if slug in all_slugs:
 # Do your magic here to make it unique
 return slug
}}}

-- 
George


Re: Problem with overriding save() method

2009-05-07 Thread Lee Hinde

On Wed, May 6, 2009 at 11:37 PM, George Song  wrote:
>
> On 5/6/2009 11:18 PM, Lee Hinde wrote:
>> On Wed, May 6, 2009 at 11:15 PM, Lee Hinde  wrote:
>>> On Wed, May 6, 2009 at 10:54 PM, George Song  wrote:
 On 5/6/2009 10:34 PM, Lee Hinde wrote:
> On Wed, May 6, 2009 at 10:22 PM, George Song  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
-~--~~~~--~~--~--~---



Re: Problem with overriding save() method

2009-05-07 Thread George Song

On 5/6/2009 11:18 PM, Lee Hinde wrote:
> On Wed, May 6, 2009 at 11:15 PM, Lee Hinde  wrote:
>> On Wed, May 6, 2009 at 10:54 PM, George Song  wrote:
>>> On 5/6/2009 10:34 PM, Lee Hinde wrote:
 On Wed, May 6, 2009 at 10:22 PM, George Song  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

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



Re: Problem with overriding save() method

2009-05-07 Thread Lee Hinde

On Wed, May 6, 2009 at 11:15 PM, Lee Hinde  wrote:
> On Wed, May 6, 2009 at 10:54 PM, George Song  wrote:
>>
>> On 5/6/2009 10:34 PM, Lee Hinde wrote:
>>> On Wed, May 6, 2009 at 10:22 PM, George Song  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
-~--~~~~--~~--~--~---



Re: Problem with overriding save() method

2009-05-07 Thread Lee Hinde

On Wed, May 6, 2009 at 10:54 PM, George Song  wrote:
>
> On 5/6/2009 10:34 PM, Lee Hinde wrote:
>> On Wed, May 6, 2009 at 10:22 PM, George Song  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.

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



Re: Problem with overriding save() method

2009-05-06 Thread George Song

On 5/6/2009 10:34 PM, Lee Hinde wrote:
> On Wed, May 6, 2009 at 10:22 PM, George Song  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

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



Re: Problem with overriding save() method

2009-05-06 Thread Lee Hinde

On Wed, May 6, 2009 at 10:22 PM, George Song  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.

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



Re: Problem with overriding save() method

2009-05-06 Thread George Song

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

-- 
George

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



Problem with overriding save() method

2009-05-06 Thread Lee Hinde

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.

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.

 - Lee

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



Re: Overriding save method in model

2009-05-06 Thread Thierry

I don't exactly want to save the full path of the image in the
database, just the image extension.  I am trying to convert the
current implementation of a PHP web page to Django.  Here's how the
table looks like, I'll be more explicit with the img ext this time:

idname  img_ext
-
1Pluto   jpg
2Scooby   gif

I know in advance that all my pet images will be stored at the
location /usr/django/images/.  So, I can locate the Pluto image with
its unique id.
Pluto: /usr/django/images/1.jpg
Scooby: /usr/django/images/2.gif

I was thinking the best way to do the above is by overriding the save
method.  What I have in mind is:
- Upload the image using ImageField
- Update the column img_ext to the image extension of the above image

The original image upload mechanism might look strange to some but
that's how it is and I am just doing a simple conversion to Django.

On May 6, 2:59 am, Daniel Roseman 
wrote:
> On May 5, 10:15 pm, Thierry  wrote:
>
> > How can I set picture to the image extension?  I don't think
> > "instance.picture = ext" works:
>
> >  def pet_picture_upload(instance, filename):
> >      name, ext = os.path.splitext(filename)
> >      instance.picture = ext
> >      return '/usr/django/images/%s%s' % (instance.pk, ext)
>
> I don't understand what you mean. The value stored in the database for
> 'picture' is the return value of the pet_picture_upload function,
> which is where the picture is stored. It doesn't make sense to set
> 'picture' to just the extension, since there will be no way for Django
> to identify the actual image file. The original code I gave sets
> 'picture' to the value of /usr/django/images/.ext, which appears
> to be what you want.
>
> If you really want to store the extension separately, you can do that
> - probably also in the pet_picture_upload function, by having a
> separate field for 'ext' and assigning it there, but I don't see why
> you would want to.
> --
> DR.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Overriding save method in model

2009-05-06 Thread Daniel Roseman

On May 5, 10:15 pm, Thierry  wrote:
> How can I set picture to the image extension?  I don't think
> "instance.picture = ext" works:
>
>  def pet_picture_upload(instance, filename):
>      name, ext = os.path.splitext(filename)
>      instance.picture = ext
>      return '/usr/django/images/%s%s' % (instance.pk, ext)

I don't understand what you mean. The value stored in the database for
'picture' is the return value of the pet_picture_upload function,
which is where the picture is stored. It doesn't make sense to set
'picture' to just the extension, since there will be no way for Django
to identify the actual image file. The original code I gave sets
'picture' to the value of /usr/django/images/.ext, which appears
to be what you want.

If you really want to store the extension separately, you can do that
- probably also in the pet_picture_upload function, by having a
separate field for 'ext' and assigning it there, but I don't see why
you would want to.
--
DR.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Overriding save method in model

2009-05-05 Thread Thierry

How can I set picture to the image extension?  I don't think
"instance.picture = ext" works:

 def pet_picture_upload(instance, filename):
 name, ext = os.path.splitext(filename)
 instance.picture = ext
 return '/usr/django/images/%s%s' % (instance.pk, ext)

On May 5, 3:48 pm, Daniel Roseman 
wrote:
> On May 5, 8:00 pm, Thierry  wrote:
>
>
>
> > I have the following model:
>
> > class Pet(models.Model):
> >     name = models.CharField(max_length=64)
> >     picture = models.ImageField(upload_to='/usr/django/images/')
>
> >     def save(self, force_insert=False, force_update=False):
> >         // override the picture values
> >         super(Pet, self).save(force_insert, force_update) # Call the
> > "real" save() method.
>
> > I want to achieve the following:
> > - Any uploaded pet image will have the following name on the file
> > system ., for example
> >     /usr/django/images/1.jpg, /usr/django/images/2.gif
> > - How can can I retrieve the image extension, override the save method
> > and store it in the column picture?
>
> > The Pet table will look something like:
> > id       name            picture
> > 1        Pluto             jpg
> > 2        Scooby         gif
>
> > All the above is done on the admin side.
>
> Don't do this in the save method. Instead, define a custom upload
> handler for the picture element.
>
> import os.path
>
> def pet_picture_upload(instance, filename):
>     name, ext = os.path.splitext(filename)
>     return '/usr/django/images/%s%s' % (instance.pk, ext)
>
> class Pet(models.Model):
>     name = models.CharField(max_length=64)
>     picture = models.ImageField(upload_to=pet_picture_upload)
>
> See the documentation for FileField, 
> here:http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.mod...
> --
> DR.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Overriding save method in model

2009-05-05 Thread Daniel Roseman

On May 5, 8:00 pm, Thierry  wrote:
> I have the following model:
>
> class Pet(models.Model):
>     name = models.CharField(max_length=64)
>     picture = models.ImageField(upload_to='/usr/django/images/')
>
>     def save(self, force_insert=False, force_update=False):
>         // override the picture values
>         super(Pet, self).save(force_insert, force_update) # Call the
> "real" save() method.
>
> I want to achieve the following:
> - Any uploaded pet image will have the following name on the file
> system ., for example
>     /usr/django/images/1.jpg, /usr/django/images/2.gif
> - How can can I retrieve the image extension, override the save method
> and store it in the column picture?
>
> The Pet table will look something like:
> id       name            picture
> 1        Pluto             jpg
> 2        Scooby         gif
>
> All the above is done on the admin side.

Don't do this in the save method. Instead, define a custom upload
handler for the picture element.

import os.path

def pet_picture_upload(instance, filename):
name, ext = os.path.splitext(filename)
return '/usr/django/images/%s%s' % (instance.pk, ext)

class Pet(models.Model):
name = models.CharField(max_length=64)
picture = models.ImageField(upload_to=pet_picture_upload)

See the documentation for FileField, here:
http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.FileField
--
DR.


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



Overriding save method in model

2009-05-05 Thread Thierry

I have the following model:

class Pet(models.Model):
name = models.CharField(max_length=64)
picture = models.ImageField(upload_to='/usr/django/images/')

def save(self, force_insert=False, force_update=False):
// override the picture values
super(Pet, self).save(force_insert, force_update) # Call the
"real" save() method.

I want to achieve the following:
- Any uploaded pet image will have the following name on the file
system ., for example
/usr/django/images/1.jpg, /usr/django/images/2.gif
- How can can I retrieve the image extension, override the save method
and store it in the column picture?

The Pet table will look something like:
id   namepicture
1Pluto jpg
2Scooby gif

All the above is done on the admin side.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Overriding save() method on models to automatically add ManyToMany relationship

2009-04-27 Thread Adam Olsen

On Mon, Apr 27, 2009 at 11:27 AM, Alex Gaynor  wrote:

> The issues if the method, it's nonsensical and doesn't correspond to
> anything(you are instantiating models.Model with save as the first
> argument), in Python the correct way to call the parent class's method is:
>
> super(MyClass, self).save(*args, **kwargs)

It was an example, complete with a typo.  It should have been:

def save(self, *args):
  models.Model.save(self, *args)
  category = Category.objects.all()[0]
  self.categories.add(category)

However, that point is not the problem.  Even with the proper syntax,
it doesn't save the ManyToMany relationship.

-- 
Adam Olsen
SendOutCards.com
http://www.vimtips.org
http://last.fm/user/synic

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



Re: Overriding save() method on models to automatically add ManyToMany relationship

2009-04-27 Thread Alex Gaynor
On Mon, Apr 27, 2009 at 1:24 PM, Adam Olsen  wrote:

>
> I've got two models, something like this:
>
> class Category(models.Model):
>name = models.CharField(max_length=60)
>
> class Product(models.Model):
>sku = models.CharField(max_length=20)
>categories = models.ManyToManyField(Category)
>
> I want to overload the save method to automatically add certain
> products to certain categories in special cases:
>
> def save(self, *args):
>   models.Model(save, *args)
>   category = Category.objects.all()[0]
>   self.categories.add(category)
>
> This does not work, I'm sure it's saving ManyToMany relationships
> later on in the save process.  Is there a way to make this work?
>
> --
> Adam Olsen
> SendOutCards.com
> http://www.vimtips.org
> http://last.fm/user/synic
>
> >
>
The issues if the method, it's nonsensical and doesn't correspond to
anything(you are instantiating models.Model with save as the first
argument), in Python the correct way to call the parent class's method is:

super(MyClass, self).save(*args, **kwargs)

Alex

-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." --Voltaire
"The people's good is the highest law."--Cicero

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



Overriding save() method on models to automatically add ManyToMany relationship

2009-04-27 Thread Adam Olsen

I've got two models, something like this:

class Category(models.Model):
name = models.CharField(max_length=60)

class Product(models.Model):
sku = models.CharField(max_length=20)
categories = models.ManyToManyField(Category)

I want to overload the save method to automatically add certain
products to certain categories in special cases:

def save(self, *args):
   models.Model(save, *args)
   category = Category.objects.all()[0]
   self.categories.add(category)

This does not work, I'm sure it's saving ManyToMany relationships
later on in the save process.  Is there a way to make this work?

-- 
Adam Olsen
SendOutCards.com
http://www.vimtips.org
http://last.fm/user/synic

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