Hi all
I'm trying to make one of my first apps running but always getting weird
errors Attribute/Type for models.DecimalField as well as for DateField.
In case I want to store anything into sqlitedb I always get:
TypeError: 'datetime.datetime' object is unsubscriptable, same for
Decimal/int
For the case I already have any records in db and just want to display them
in admin interface:
Request Method: GET Request URL: http://127.0.0.1:8000/admin/energie/el/
Django
Version: 1.5 Exception Type: AttributeError Exception Value:
'Decimal' object has no attribute 'encode'
Exception Location:
/usr/lib/python2.6/site-packages/Django-1.5-py2.6.egg/django/db/models/base.py
in __str__, line 433 Python Executable: /usr/bin/python Python Version:
2.6.6 Python Path:
['/home/tpelka/workspace/energie/wsgi/energie',
'/usr/lib/python2.6/site-packages/ropemode-0.2-py2.6.egg',
'/usr/lib/python2.6/site-packages/rope-0.9.4-py2.6.egg',
'/usr/lib/python2.6/site-packages/ropevim-0.4-py2.6.egg',
'/usr/lib/python2.6/site-packages/mozmill-1.5.20-py2.6.egg',
'/usr/lib/python2.6/site-packages/ManifestDestiny-0.2.2-py2.6.egg',
'/usr/lib/python2.6/site-packages/mozrunner-2.5.14-py2.6.egg',
'/usr/lib/python2.6/site-packages/jsbridge-2.4.16-py2.6.egg',
'/usr/lib/python2.6/site-packages/Django-1.5-py2.6.egg',
'/usr/share/qa-tools/python-modules',
'/usr/lib64/python26.zip',
'/usr/lib64/python2.6',
'/usr/lib64/python2.6/plat-linux2',
'/usr/lib64/python2.6/lib-tk',
'/usr/lib64/python2.6/lib-old',
'/usr/lib64/python2.6/lib-dynload',
'/usr/lib64/python2.6/site-packages',
'/usr/lib64/python2.6/site-packages/Numeric',
'/usr/lib64/python2.6/site-packages/gst-0.10',
'/usr/lib64/python2.6/site-packages/gtk-2.0',
'/usr/lib64/python2.6/site-packages/webkit-1.0',
'/usr/lib/python2.6/site-packages',
'/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info']
Attaching models,py, admin.py and settings.py
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
# -*- coding: utf-8 -*-
from django.contrib import admin
from models import Record, Water, Gas, El
class RecordAdmin(admin.ModelAdmin):
list_display = ('date', 'water', 'gas', 'el', 'note')
search_fields = ['date']
admin.site.register(Record, RecordAdmin)
admin.site.register(Water)
admin.site.register(Gas)
admin.site.register(El)
# -*- coding: utf-8 -*-
from django.db import models
from decimal import Decimal
class Record(models.Model):
date = models.DateField('?as zad?n?')
water = models.ForeignKey('Water', verbose_name='Voda')
gas = models.ForeignKey('Gas', verbose_name='Plyn')
el = models.ForeignKey('El', verbose_name='Elekt?ina')
note = models.TextField('Pozn?mka', blank=True)
def __unicode__(self):
return self.date
class Meta:
ordering = ['date']
verbose_name = 'z?znam'
verbose_name_plural = 'z?znamy'
class Water(models.Model):
water = models.DecimalField('Spot?eba vody', max_digits=5, decimal_places=1)
water_delta = models.DecimalField('Spot?eba vody rozd?l',
max_digits=4, decimal_places=1, default=1)
def __unicode__(self):
return self.water
class Meta:
verbose_name = 'voda'
verbose_name_plural = 'voda'
class Gas(models.Model):
gas = models.DecimalField('Spot?eba plynu', max_digits=8, decimal_places=1)
gas_delta = models.DecimalField('Spot?eba plynu rozd?l',
max_digits=4, decimal_places=1, default=1)
def __unicode__(self):
return self.gas
class Meta:
verbose_name = 'plyn'
verbose_name_plural = 'plyn'
class El(models.Model):
el1 = models.DecimalField('Spot?eba elekt?iny', max_digits=8, decimal_places=1)
el1_delta = models.DecimalField('Spot?eba elekt?iny rozd?l',
max_digits=4, decimal_places=1, default=1)
el2 = models.DecimalField('Spot?eba elekt?iny (no?n? proud)',
max_digits=8, decimal_places=1)
el2_delta = models.DecimalField('Spot?eba elekt?iny (no?n? proud) rozd?l',
max_digits=4, decimal_places=1, default=1)
def __unicode__(self):
return self.el1
class Meta:
verbose_name = 'elekt?ina'
verbose_name_plural = 'elekt?ina'
# -*- coding: utf-8 -*-
# Django settings for openshift project.
import imp, os
# a setting to determine whether we are running on OpenShift
ON_OPENSHIFT = False
if os.environ.has_key('OPENSHIFT_REPO_DIR'):
ON_OPENSHIFT = True
PROJECT_DIR = os.path.dirname(os.path.realpath(__file__))
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
('Tomas Pelka', '[email protected]'),
)
MANAGERS = ADMINS
if ON_OPENSHIFT:
# os.environ['OPENSHIFT_MYSQL_DB_*'] variables can be used with databases created
# with rhc cartridge add (see /README in this git repo)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ['OPENSHIFT_APP_NAME'],
'USER': os.environ['OPENSHIFT_DB_USERNAME'],
'PASSWORD': os.environ['OPENSHIFT_DB_PASSWORD'],
'HOST': os.environ['OPENSHIFT_DB_HOST'],
'PORT': os.environ['OPENSHIFT_DB_PORT'],
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': os.path.join(PROJECT_DIR, 'sqlite3.db'), # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# On Unix systems, a value of None will cause Django to use the same
# timezone as the operating system.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'Europe/Prague'
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'cs'
DEFAULT_CHARSET = 'utf-8'
SITE_ID = 1
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale
USE_L10N = True
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = os.environ.get('OPENSHIFT_DATA_DIR', '')
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = ''
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = os.path.join(PROJECT_DIR, '..', 'static')
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# URL prefix for admin static files -- CSS, JavaScript and images.
# Make sure to use a trailing slash.
# Examples: "http://foo.com/static/admin/", "/static/admin/".
ADMIN_MEDIA_PREFIX = '/static/admin/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
# Make a dictionary of default keys
default_keys = { 'SECRET_KEY': 'vm4rl5*ymb@2&d_(gc$gb-^twq9w(u69hi--%$5xrh!xk(t%hw' }
# Replace default keys with dynamic values if we are in OpenShift
use_keys = default_keys
if ON_OPENSHIFT:
imp.find_module('openshiftlibs')
import openshiftlibs
use_keys = openshiftlibs.openshift_secure(default_keys)
# Make this unique, and don't share it with anybody.
SECRET_KEY = use_keys['SECRET_KEY']
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
#'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
ROOT_URLCONF = 'energie.urls'
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(PROJECT_DIR, 'templates'),
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'energie',
)
# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
}
}