> 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 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().
>
> I have suggested a pre_save / post_save hooks for ModelForm with some
> code recently 
> here:http://groups.google.com/group/django-developers/browse_thread/thread...
>
> -Alen Ribic
>
> On May 6, 6:50 pm, Jashugan <[EMAIL PROTECTED]> wrote:
>
> > 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 =
> > StorageUnitType.objects.get(pk=parent).total_slots
> >         if position == None:
> >             position = 1
> >         super(StorageUnit, self).save()
>
> > Is this the best way of doing this? Is there a way I can specify that
> > this code should run only when a model is first saved? I read
> > something about using listeners for some post and pre save signals,
> > but wasn't able to find any documentation on it.
>
> > TIA
--~--~---------~--~----~------------~-------~--~----~
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