Code

Ticket #3538: filefield_basename_trunk.diff

File filefield_basename_trunk.diff, 1.4 KB (added by Herbert Poul <herbert.poul@…>, 7 years ago)

simple patch for latest svn trunk

Line 
1Index: django/db/models/base.py
2===================================================================
3--- django/db/models/base.py    (revision 4549)
4+++ django/db/models/base.py    (working copy)
5@@ -313,6 +313,11 @@
6             return os.path.join(settings.MEDIA_ROOT, getattr(self, field.attname))
7         return ''
8 
9+    def _get_FIELD_basename(self, field):
10+        if getattr(self, field.attname): # value is not blank
11+           return os.path.basename( self._get_FIELD_filename(field) )
12+       return ''
13+
14     def _get_FIELD_url(self, field):
15         if getattr(self, field.attname): # value is not blank
16             import urlparse
17Index: django/db/models/fields/__init__.py
18===================================================================
19--- django/db/models/fields/__init__.py (revision 4549)
20+++ django/db/models/fields/__init__.py (working copy)
21@@ -631,6 +631,7 @@
22         setattr(cls, 'get_%s_filename' % self.name, curry(cls._get_FIELD_filename, field=self))
23         setattr(cls, 'get_%s_url' % self.name, curry(cls._get_FIELD_url, field=self))
24         setattr(cls, 'get_%s_size' % self.name, curry(cls._get_FIELD_size, field=self))
25+       setattr(cls, 'get_%s_basename' % self.name, curry(cls._get_FIELD_basename, field=self))
26         setattr(cls, 'save_%s_file' % self.name, lambda instance, filename, raw_contents: instance._save_FIELD_file(self, filename, raw_contents))
27         dispatcher.connect(self.delete_file, signal=signals.post_delete, sender=cls)
28