Skip to content

Instantly share code, notes, and snippets.

@ymph
Last active August 29, 2015 14:21
Show Gist options
  • Save ymph/cd2b684f3f2f38a680d5 to your computer and use it in GitHub Desktop.
Save ymph/cd2b684f3f2f38a680d5 to your computer and use it in GitHub Desktop.
Django FileField default callable problem

FileField callable default problem

How to reproduce:

  • create a virtualenv and activate it. Install Django (pip install django)

  • create a new testdefault project : django-admin startproject testdefault

  • copy the gist file into the testdefault folder (i.e. files admin.py, models.py, settings.py)

  • run the following commands:

    $ python manage.py migrate
    $ python manage.py createsuperuser
    $ python manage.py runserver
    
  • navigate to http://localhost:8000/admin/

  • when creating "Test objects" we have the error 'function' object has no attribute '_committed' in django/db/models/fields/files.py in pre_save, line 313

  • when creating "Test object strs" no problem.

  • the only difference between them is the teh value of the default attribute of their FileField field. One (TestObjectStr) is a simple string, the other (TestObject) is a callable returning the same string.

  • To note: this bug does not appear when directly creating objects in code. It seems related to the form management. You can test it by running a django shell. This runs without problem, with the expected default value:

    $ python manage.py shell
      >>> from testdefault import models
      >>> models.TestObject.objects.create()
    
from django.contrib import admin
from .models import TestObject, TestObjectStr
admin.site.register(TestObject)
admin.site.register(TestObjectStr)
from django.db import models
def get_test_object_default():
return "path/to/some/file"
class TestObject(models.Model):
pub_file = models.FileField(default=get_test_object_default)
class TestObjectStr(models.Model):
pub_file = models.FileField(default="path/to/some/file")
"""
Django settings for testdefault project.
Generated by 'django-admin startproject' using Django 1.8.1.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'nkd^!*%vhu)e!_c1^&6m1-kfhz9upewo-qnqurh_w)y01!e^b2'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'testdefault'
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'testdefault.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'testdefault.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment