Django

Code

Ticket #3621 (closed: fixed)

Opened 3 years ago

Last modified 2 years ago

FileField in admin broken with sqlite3

Reported by: jimmy@example.com Assigned to: Gulopine
Milestone: 1.0 beta Component: Database layer (models, ORM)
Version: SVN Keywords: sqlite3 FileField unique admin, fs-rf-fixed
Cc: Triage Stage: Accepted
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description (Last modified by russellm)

Using the sqlite3 backend:

If FileField? has unique=True set, you cannot add a file due to an InterfaceError?: Error binding parameter 0 - probably unsupported type.

If you drop the unique=True flag, you are able to insert a object, but not change it; the admin interface gives: Enter a valid filename.

The traceback when unique=True:

Traceback (most recent call last):
File "/Users/jimmy/UNIX/src/django-devel/django/core/handlers/base.py" in get_response
  77. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/jimmy/UNIX/src/django-devel/django/contrib/admin/views/decorators.py" in _checklogin
  55. return view_func(request, *args, **kwargs)
File "/Users/jimmy/UNIX/src/django-devel/django/views/decorators/cache.py" in _wrapped_view_func
  39. response = view_func(request, *args, **kwargs)
File "/Users/jimmy/UNIX/src/django-devel/django/contrib/admin/views/main.py" in add_stage
  250. errors = manipulator.get_validation_errors(new_data)
File "/Users/jimmy/UNIX/src/django-devel/django/db/models/fields/__init__.py" in manipulator_validator_unique
  37. old_obj = self.manager.get(**{lookup_type: field_data})
File "/Users/jimmy/UNIX/src/django-devel/django/db/models/manager.py" in get
  73. return self.get_query_set().get(*args, **kwargs)
File "/Users/jimmy/UNIX/src/django-devel/django/db/models/query.py" in get
  248. obj_list = list(clone)
File "/Users/jimmy/UNIX/src/django-devel/django/db/models/query.py" in __iter__
  108. return iter(self._get_data())
File "/Users/jimmy/UNIX/src/django-devel/django/db/models/query.py" in _get_data
  468. self._result_cache = list(self.iterator())
File "/Users/jimmy/UNIX/src/django-devel/django/db/models/query.py" in iterator
  181. cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params)
File "/Users/jimmy/UNIX/src/django-devel/django/db/backends/util.py" in execute
  12. return self.cursor.execute(sql, params)
File "/Users/jimmy/UNIX/src/django-devel/django/db/backends/sqlite3/base.py" in execute
  92. return Database.Cursor.execute(self, query, params)

  InterfaceError at /admin/polls/bar/add/
  Error binding parameter 0 - probably unsupported type.

Attachments

Change History

03/01/07 18:44:03 changed by Gary Wilson <gary.wilson@gmail.com>

  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • needs_tests changed.
  • needs_docs changed.

could an admin please correct the description formatting.

08/31/07 15:32:53 changed by anonymous

This is screwing me up too, I get this error when saving an instance of a model with sqlite3 that has certain fields with default values. Works in the admin but not in my code or the shell

09/22/07 07:34:48 changed by russellm

  • description changed.

12/11/07 13:37:05 changed by Gulopine

  • keywords changed from sqlite3 FileField unique admin to sqlite3 FileField unique admin, fs-rf.

12/16/07 21:11:31 changed by Gulopine

  • keywords changed from sqlite3 FileField unique admin, fs-rf to sqlite3 FileField unique admin, fs-rf-fixed.

03/23/08 14:11:13 changed by rwl@alumni.upenn.edu

I'm seeing the same (or similar) behavior running trunk with a sqlite3 backend: I can't save an existing object from the admin interface once I've uploaded a file, whether or not I try to upload a new file.

The error is apparently raised in django/db/models/fields/init.py :

      def isWithinMediaRoot(field_data, all_data):
            f = os.path.abspath(os.path.join(settings.MEDIA_ROOT, field_data))
            if not f.startswith(os.path.abspath(os.path.normpath(settings.MEDIA_ROOT))):
                raise validators.ValidationError, _("Enter a valid filename.")

I've confirmed that the error is raised because field_data contains a leading slash, so that os.path.join(...) returns a path more like the file's URL than its filesystem path; that is, os.path.join(settings.MEDIA_ROOT, field_data) returns a string identical to field_data itself. So, if field_data is "/static/img/ukulele.jpg", then f is "/static/img/ukulele.jpg", instead of, e.g., "/home/mymediaroot/static/img/ukulele.jpg".

I hope that's helpful. I'm not sure exactly why it would be specific to the sqlite3 backend, but I suppose it must have something to do with the way FileField? data is translated to the string stored in the database. If someone has suggestions on where to go from here, I'd be happy to look into it further.

06/16/08 13:29:19 changed by Gulopine

  • milestone set to 1.0 beta.

06/18/08 11:38:12 changed by felix@crucial-systems.com

It happens with MySQL too. You cannot edit or save at all because the file field is always declared illegal once there is something there.

the file name saved has a starting slash: /releases/verve.jpg

and I guess that it shouldn't

06/18/08 12:56:21 changed by anonymous

I had the same with mysql. the real error was that upload_to='/releases/' was invalid

it failed to save file (and yet did not throw an error)

it threw a false validation error "Please enter a valid filename" - the real problem is the path is invalid which is due to a misconfiguration error.

06/18/08 12:58:06 changed by anonymous

by invalid I meant there should be no slashes before or after

wrong: upload_to='/releases/'

right: upload_to='releases'

sorry for multiple posts, Trac threw away ("cookie error") my better written original post.

06/18/08 13:29:37 changed by Gulopine

My apologies for not explaining, but the fs-rf-fixed keyword on this ticket means that my work on #5361 addresses this issue. The plan is to raise a SuspiciousOperation exception if you do this, since you're technically telling Django to save a file somewhere outside your MEDIA_ROOT setting, which will no longer be allowed. It still won't give a validation error when you use manage.py validate, but the error that comes up when you submit a file will be more meaningful.

In the /releases/ example, the error would be:

SuspiciousOperation: Attempted access to '/releases/filename' denied.

06/18/08 14:04:24 changed by jacob

  • reporter changed from jimmy@inet-solutions.be to jimmy@example.com.

Redacted reporter's name by request.

07/31/08 08:01:47 changed by Gulopine

  • owner changed from nobody to Gulopine.
  • status changed from new to assigned.

08/08/08 15:59:02 changed by jacob

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [8244]) File storage refactoring, adding far more flexibility to Django's file handling. The new files.txt document has details of the new features.

This is a backwards-incompatible change; consult BackwardsIncompatibleChanges for details.

Fixes #3567, #3621, #4345, #5361, #5655, #7415.

Many thanks to Marty Alchin who did the vast majority of this work.


Add/Change #3621 (FileField in admin broken with sqlite3)




Change Properties
Action