Re: in my model, how can I tell when I'm saving a new item

2011-07-25 Thread Ian Clelland
On Mon, Jul 25, 2011 at 4:05 PM, webcomm wrote: > > Thanks. Any idea why the following isn't working in my News class? > > The processing doesn't get to that exit call, even if it's a new item. > > > > def save(self, *args, **kwargs): > > if not hasattr(self,'id'): > > im

Re: in my model, how can I tell when I'm saving a new item

2011-07-25 Thread webcomm
> Thanks.  Any idea why the following isn't working in my News class? > The processing doesn't get to that exit call, even if it's a new item. > >     def save(self, *args, **kwargs): >         if not hasattr(self,'id'): >             import sys >             sys.exit('must be a new item') >      

Re: in my model, how can I tell when I'm saving a new item

2011-07-25 Thread webcomm
On Jul 25, 3:57 pm, Javier Guerra Giraldez wrote: > On Mon, Jul 25, 2011 at 2:50 PM, webcomm wrote: > > try: > >  self.id > > except NameError: > >  # run this code if it is a new item > > else: > >  # run this code if this is a previously saved item > > works, but it's nicer to use "if hasattr(o

Re: in my model, how can I tell when I'm saving a new item

2011-07-25 Thread Javier Guerra Giraldez
On Mon, Jul 25, 2011 at 2:50 PM, webcomm wrote: > try: >  self.id > except NameError: >  # run this code if it is a new item > else: >  # run this code if this is a previously saved item works, but it's nicer to use "if hasattr(obj,field): " > More generally, what do you think of this state

Re: in my model, how can I tell when I'm saving a new item

2011-07-25 Thread webcomm
I'm still learning python, so have to ask about how this would be done. Something like the following? try: self.id except NameError: # run this code if it is a new item else: # run this code if this is a previously saved item More generally, what do you think of this statement: "If there

Re: in my model, how can I tell when I'm saving a new item

2011-07-25 Thread Dmitry Pisklov
Have a look at this doc: https://docs.djangoproject.com/en/1.3/ref/models/instances/#how-django-knows-to-update-vs-insert Basically, it says, that given you're not going to assign primary key prior to saving your object to DB, you may rely on PK field's value, e.g. if it's None - it's new model,

Re: in my model, how can I tell when I'm saving a new item

2011-07-25 Thread Javier Guerra Giraldez
On Mon, Jul 25, 2011 at 2:03 PM, webcomm wrote: > I want to override the save method in one of my models.  How do I know > if I'm calling save on a newly created item or on one that is already > in the database?  How to make that distinction in my save method? if it has an id, it's not new -- J

in my model, how can I tell when I'm saving a new item

2011-07-25 Thread webcomm
I want to override the save method in one of my models. How do I know if I'm calling save on a newly created item or on one that is already in the database? How to make that distinction in my save method? Thanks, Ryan -- You received this message because you are subscribed to the Google Groups