>
> Question 2: 
> How do I selectively delete the "image" field without deleting the 
> entire "test" record? In other words, I want to delete the image file 
> and null-out the image field, but leave the rest of the test record 
> intact.


I suppose you would have to explicitly delete the file. This is made a 
little more complicated by uploadseparate=True. In that case, the file is 
stored two sub-folders deep inside the field's uploadfolder -- the path to 
the file is [uploadfolder]/[tablename].[fieldname]/[first 2 characters of 
UUID portion of filename]. For example, the filename might be something 
like test.name.9d4498f37ce2b906.746573742066696c652e6a7067.jpg (the 
highlighted segment after the table and field name is the UUID fragment). 
The path to that file would be 
/static/data/test.name/9d/test.name.9d4498f37ce2b906.746573742066696c652e6a7067.jpg.
 
To delete the file associated with a particular record (warning: all code 
below untested):

file_to_delete = db(db.test.id==[id to delete]).select().first().name
import os
os.remove(os.path.join(db.test.name.uploadfolder, 'test.name',
    file_to_delete.split('.')[2][:2], file_to_delete))

You could also abstract this into a general function that also updates the 
db record:

def delete_file(row, uploadfield):
    import os
    file = row[uploadfield]
    table, field, subfolder = file.split('.')[0:3]
    subfolder = subfolder[:2]
    os.remove(os.path.join(db[table][field].uploadfolder,
        '%s.%s' % (table, field), subfolder, file))
    row.update_record(**{uploadfield: None})

Anthony

Reply via email to