You could do something like this:
def save(self, force_insert=False, force_update=False):
# first save the model using the base class save method
super(MyModel, self).save(force_insert, force_update)
# then self will have an id set
v = self.id
On Tue, 2009-10-06 at 09:05 -0700, Aaron wrote:
> When saving a model, I need to be able to access its ID field like
> this:
>
> class MyModel:
> ...
> def save(self, force_insert = False, force_update = False):
> v = self.id
> # Do something with v
>
> super(MyModel, self).save(force_insert, force_update)
>
> However, if the model is being created for the first time, self.id is
> None, so I need a way to find out what the ID is going to be.
>
> So far I've come up with this:
>
> class MyModel:
> ...
> def save(self, force_insert = False, force_update = False):
> v = self.id
>
> if not v:
> if MyModel.objects.count() > 0: # Do other MyModel objects
> already exist?
> v = MyModel.objects.latest(field_name = 'id').id + 1
> else:
> v = 1
>
> # Do something with v
>
> super(MyModel, self).save(force_insert, force_update)
>
> Am I calculating the correct ID here?
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---