Login

Prefetch id for Postgresql backend

Author:
bretth
Posted:
April 17, 2012
Language:
Python
Version:
Not specified
Tags:
models filefield
Score:
-1 (after 1 ratings)

When uploading a file or image, you need to put it somewhere that's not going to be orphaned by a change in the model. You could use a globally unique value like a uuid, but the django id autofield is a much shorter surrogate field that can be used in a friendly path to the object. The problem with id autofields is that they don't exist when the object is created for the first time - so all files using the id get uploaded to a location 'None' on first save, increasing the likelihood of name collisions.

This postgresql only snippet fetches the next id in a way that is safe for concurrent access.

This snippet only works on postgresql because neither sqlite or mysql have a nextval equivalent. Instead you would have to lock the table for writes and select the last value inserted incrementing it yourself.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import os
from django.db import connection, models

def prefetch_id(instance):
    """ Fetch the next value in a django id autofield postgresql sequence """
    cursor = connection.cursor()
    cursor.execute(
        "SELECT nextval('{0}_{1}_id_seq'::regclass)".format(
            instance._meta.app_label.lower(),
            instance._meta.object_name.lower(),
        )
    )
    row = cursor.fetchone()
    cursor.close()
    return int(row[0])

def make_file_path(instance, filename):
    return os.path.join(instance._meta.app_label, instance._meta.object_name,  instance.id,filename)

class SomeModel(models.Model):

    some_file = FileField(upload_to=make_file_path)

    def save(self, *args, **kwargs):
        if not self.id and some_file:
            self.id = prefetch_id(instance)

        super(SomeModel, self).save(*args, **kwargs)

More like this

  1. Image uploader to use with file uploader from http://github.com/valums/file-uploader by tgdn 3 years, 6 months ago
  2. Improved Pickled Object Field by taavi223 5 years, 10 months ago
  3. Admin page without inlines dinamically by davmuz 3 years ago
  4. Function/Stored Procedure Manager by axiak 8 years ago
  5. load m2m fields objects by dirol 5 years ago

Comments

Please login first before commenting.