> I should be able to do it with just one
> hit at the database. Second (and subsequent) hit(s) should occur only
> in the case of an insert exception (AssertionFailed). But the way
> Django doesn't differentiate b/w INSERTs and UPDATEs, I have to
> manually check for collisions and hit the database TWO TIMES for every
> new entry (that's silly).

I see his point here. The code would use less Queries if written this way:
-----------------------------------------------------------------------
if not this.id:
  while True:
    try:
      this.id = uuid_stuff()
      # throws exception if id exists, as this field is unique
      return this._insert()
    except:
      pass
else:
  return this._update()
-----------------------------------------------------------------------

Perhaps save() could be change to actually do this:
(As I started writing my own DB-abstraction-layer once, I know this can
become very handy)
-----------------------------------------------------------------------
def save(self): # simplified
  if self.id:
    self._update()
  else:
    self._insert()
-----------------------------------------------------------------------

> I wanted to use UUID as primary key in my models. Since Model doesn't
> have any add() method, I have to do it like this:

I would recommend using a different field for the uuid:
-----------------------------------------------------------------------
class MyModel(models.Model):
  uuid = models.CharField(maxlength=X, unique=True)
  def save(self):
    if not this.uuid:
      while True:
        try:
          this.uuid = uuid_stuff()
          return super(MyModel, self).save()
        except:
          pass
    else:
      return super(MyModel, self).save()
-----------------------------------------------------------------------

> Adding a new entry
> and updating an existing one are _different_ operations and the should
> not be *automagically* fused together. Even underlying SQL is
> separate!

Here you are wrong. It really should be fused, as this makes things
really easy. But I think allowing the user to explicit use UPDATE or
INSERT would be a nice feature.

Greetings, David Danier

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@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-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to