I don't know about horrible, it depends what you're going for. I was
sort of suggesting that the default behaviour for file fields and image
fields is wrong, and changing it fundamentally would be the way to go.

Overriding save() is much cleaner, you're right. But I don't think the
test should be:

if self.name in Photo.objects.all()

This will bring the entire table backing the photo objects into django,
which is quite slow if there's a network link between django and the
database. It could also eat memory like a grue.

Moving processing to the app server is good, but I think filter() and
count() would work better:

class Photo( Model )
  def save( self ):
      c = Photo.objects.all().filter( name = self.name ).count()
      if c > 0:
          self.name += '_' + str( c + 1 )
      Model.save( self )

Afaik, filter() will change the SQL query django sends to the database
when count() is called to only examine rows in that have name equal to
self.name. If name is indexed, the query can be blazingly fast for very
large tables. If it's not indexed you'll get the same hit on the
database, but reduce the work django has to do, and reduce network
traffic to bout.

This is cleaner, but overriding save() has the disadvantage of
potentially generating names like "image_2__.gif" if files exist in the
filesystem that don't have corresponding records in the database.

Cheers,
~ol


--~--~---------~--~----~------------~-------~--~----~
 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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to