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 django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to