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
> 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')
>
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
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
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
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,
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
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
8 matches
Mail list logo