Re: Setting attributes before model is created

2008-05-07 Thread Jashugan
On May 6, 4:44 pm, "Russell Keith-Magee" <[EMAIL PROTECTED]> wrote: > Well - there is one other way: the default argument on a field. For example: > >    position = models.IntegerField(blank=True, default=1) > > You can also pass a callable: > > from datetime import datetime > ... >    pub_date =

Re: Setting attributes before model is created

2008-05-06 Thread Russell Keith-Magee
On Wed, May 7, 2008 at 1:00 AM, Alen Ribic <[EMAIL PROTECTED]> wrote: > > At this moment, to my knowledge, the only way to do this is by > overriding the save() method. Within your save, run the pre-save work > and then return the super.save(). Well - there is one other way: the default argume

Re: Setting attributes before model is created

2008-05-06 Thread Alen Ribic
Signals rule. :-) Gave it a test drive and works like a charm. from django.db import models from django.db.models import signals from django.dispatch import dispatcher def send_entry_created_email(sender, instance, signal, *args, **kwargs): if instance.id is None: # load 'create' m

Re: Setting attributes before model is created

2008-05-06 Thread Alen Ribic
So something like this should do it: from django.db import models from django.db.models import signals from django.dispatch import dispatcher def send_entry_created_email(): # do some work... class Entry(models.Model): # ... dispatcher.connect(send_entry_created_email, signal=signa

Re: Setting attributes before model is created

2008-05-06 Thread Jashugan
On May 6, 10:40 am, Alen Ribic <[EMAIL PROTECTED]> wrote: > > Not to sure though how one registers these signals. > This seems somewhat comprehensive: http://www.mercurytide.co.uk/whitepapers/django-signals/ --~--~-~--~~~---~--~~ You received this message becaus

Re: Setting attributes before model is created

2008-05-06 Thread Alen Ribic
> Do you know anything about pre_save and post_save signals that occur > during a save? They are mentioned > here:http://www.djangoproject.com/documentation/db-api/#what-happens-when-..., > but are not expounded. To be honest, I didn't. However I took a look at it and there are indeed signal hoo

Re: Setting attributes before model is created

2008-05-06 Thread Alen Ribic
*correction: I was thinking of ModelForm instead of Model. It should be self.pk instead. if self.pk is None: # new model else: # existing model -Alen On May 6, 7:25 pm, Jashugan <[EMAIL PROTECTED]> wrote: > On May 6, 10:20 am, Alen Ribic <[EMAIL PROTECTED]> wrote: > > > > Is there a way

Re: Setting attributes before model is created

2008-05-06 Thread Jashugan
On May 6, 10:20 am, Alen Ribic <[EMAIL PROTECTED]> wrote: > > Is there a way I can specify that > > this code should run only when a model is first saved? > > in save() method, you can check if this is a new model like so: > if self.instance.pk is None: >     # new model > else: >     # existing

Re: Setting attributes before model is created

2008-05-06 Thread Jashugan
Thanks, Alex. Do you know anything about pre_save and post_save signals that occur during a save? They are mentioned here: http://www.djangoproject.com/documentation/db-api/#what-happens-when-you-save, but are not expounded. Also I modified my code (under the assumption that pk would be None for

Re: Setting attributes before model is created

2008-05-06 Thread Alen Ribic
> Is there a way I can specify that > this code should run only when a model is first saved? in save() method, you can check if this is a new model like so: if self.instance.pk is None: # new model else: # existing model Regards, -Alen Ribic On May 6, 7:00 pm, Alen Ribic <[EMAIL PROTEC

Re: Setting attributes before model is created

2008-05-06 Thread Alen Ribic
At this moment, to my knowledge, the only way to do this is by overriding the save() method. Within your save, run the pre-save work and then return the super.save(). I have suggested a pre_save / post_save hooks for ModelForm with some code recently here: http://groups.google.com/group/django-d

Setting attributes before model is created

2008-05-06 Thread Jashugan
I'd like to set some attributes to some default values before the model is created. Here's what I have so far: slots_available = models.IntegerField(blank=True) position = models.IntegerField(blank=True) def save(self): if slots_availalbe == None: slots_available