Skip to content

Instantly share code, notes, and snippets.

@ymph
Last active August 29, 2015 14:21

Revisions

  1. ymph revised this gist May 20, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -19,8 +19,10 @@ How to reproduce:
    - 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()
    ```
  2. ymph revised this gist May 20, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -7,11 +7,13 @@ How to reproduce:
    - 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.
  3. ymph revised this gist May 20, 2015. 2 changed files with 1 addition and 2 deletions.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ How to reproduce:
    - 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:
    ```bash:
    ```
    $ python manage.py migrate
    $ python manage.py createsuperuser
    $ python manage.py runserver
    1 change: 0 additions & 1 deletion models.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    from django.db import models

    def get_test_object_default():
  4. ymph created this gist May 19, 2015.
    24 changes: 24 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    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:
    ```bash:
    $ 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()
    ```
    6 changes: 6 additions & 0 deletions admin.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    from django.contrib import admin

    from .models import TestObject, TestObjectStr

    admin.site.register(TestObject)
    admin.site.register(TestObjectStr)
    11 changes: 11 additions & 0 deletions models.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@

    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")
    103 changes: 103 additions & 0 deletions settings.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,103 @@
    """
    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/'