Skip to content
This repository was archived by the owner on Mar 7, 2024. It is now read-only.

Commit 578e0f5

Browse files
committed
Initial commit
0 parents  commit 578e0f5

File tree

13 files changed

+308
-0
lines changed

13 files changed

+308
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
indent_size = 4
7+
8+
[Makefile]
9+
indent_style = tab

.gitignore

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Local
2+
[Ll]ocal
3+
*[._-][Ll]ocal
4+
!**/vimrc.local
5+
[Ll]ocal[._-]*
6+
*[._-][Ll]ocal[._-]*
7+
8+
# Python
9+
__pycache__
10+
*.pyc
11+
.venv
12+
.eggs
13+
*.egg-info
14+
_version.py
15+
16+
# Docker
17+
docker-compose.override.yml
18+
.env
19+
20+
# Secrets
21+
secrets
22+
*.key
23+
24+
# Javascript and front-end assets
25+
node_modules
26+
*.min.js
27+
*.min.css
28+
**/static/lib
29+
30+
# Build, log or temporary assets
31+
*~
32+
~*
33+
*.tmp
34+
*.log
35+
tmp
36+
build
37+
dist
38+
.DS_Store
39+
40+
# Editors
41+
.vscode
42+
.vs

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
Import field with Django 5.0 db_default date
2+
============================================
3+
4+
## How to reproduce
5+
6+
Create Python environment:
7+
8+
python -m venv .venv
9+
.\.venv\Scripts\activate
10+
pip install -r requirements.txt
11+
12+
Create and deploy PostgreSql database:
13+
14+
createdb -U postgres bugs
15+
python manage.py migrate
16+
17+
Create super user:
18+
19+
$env:DJANGO_SUPERUSER_PASSWORD = "admin"
20+
python manage.py createsuperuser --noinput --username admin --email [email protected]
21+
22+
Run server:
23+
24+
python manage.py runserver
25+
26+
Go to [http://localhost:8000/admin/import_db_default/item/](http://localhost:8000/admin/import_db_default/item/),
27+
click `Import` and select `sample.csv`.
28+
29+
## Error
30+
31+
**AttributeError: 'DatabaseDefault' object has no attribute 'utcoffset'**
32+
33+
My environment:
34+
35+
- Windows 10 22H2 (build 19045.3930)
36+
- Python 3.11.5
37+
- PostgreSQL 15.4
38+
- Django 5.0.1
39+
- Psycopg 3.1.17
40+
- Django-import-export 3.3.6
41+
42+
Error traceback:
43+
44+
Traceback (most recent call last):
45+
File "…\import-db-default\.venv\Lib\site-packages\import_export\resources.py", line 785, in import_row
46+
diff = self.get_diff_class()(self, original, new)
47+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
File "…\import-db-default\.venv\Lib\site-packages\import_export\resources.py", line 259, in __init__
49+
self.left = self._export_resource_fields(resource, instance)
50+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
51+
File "…\import-db-default\.venv\Lib\site-packages\import_export\resources.py", line 280, in _export_resource_fields
52+
return [
53+
^
54+
File "…\import-db-default\.venv\Lib\site-packages\import_export\resources.py", line 281, in <listcomp>
55+
resource.export_field(f, instance) if instance else ""
56+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
57+
File "…\import-db-default\.venv\Lib\site-packages\import_export\resources.py", line 1066, in export_field
58+
return field.export(obj)
59+
^^^^^^^^^^^^^^^^^
60+
File "…\import-db-default\.venv\Lib\site-packages\import_export\fields.py", line 148, in export
61+
return self.widget.render(value, obj)
62+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
63+
File "…\import-db-default\.venv\Lib\site-packages\import_export\widgets.py", line 266, in render
64+
value = timezone.localtime(value)
65+
^^^^^^^^^^^^^^^^^^^^^^^^^
66+
File "…\import-db-default\.venv\Lib\site-packages\django\utils\timezone.py", line 182, in localtime
67+
if is_naive(value):
68+
^^^^^^^^^^^^^^^
69+
File "…\import-db-default\.venv\Lib\site-packages\django\utils\timezone.py", line 234, in is_naive
70+
return value.utcoffset() is None
71+
^^^^^^^^^^^^^^^
72+
AttributeError: 'DatabaseDefault' object has no attribute 'utcoffset'

import_db_default/admin.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.contrib.admin import register, ModelAdmin
2+
from import_export.admin import ImportExportMixin
3+
from .models import Item
4+
5+
@register(Item)
6+
class ItemAdmin(ImportExportMixin, ModelAdmin):
7+
list_display = ["name", "created"]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Generated by Django 5.0.1 on 2024-01-12 10:49
2+
3+
import django.db.models.functions.datetime
4+
from django.db import migrations, models
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
initial = True
10+
11+
dependencies = [
12+
]
13+
14+
operations = [
15+
migrations.CreateModel(
16+
name='Item',
17+
fields=[
18+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
19+
('name', models.CharField(max_length=100)),
20+
('created', models.DateTimeField(auto_now_add=True, db_default=django.db.models.functions.datetime.Now())),
21+
],
22+
),
23+
]

import_db_default/migrations/__init__.py

Whitespace-only changes.

import_db_default/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.db import models
2+
from django.db.models.functions import Now
3+
4+
class Item(models.Model):
5+
name = models.CharField(max_length=100)
6+
created = models.DateTimeField(auto_now_add=True, db_default=Now())

import_db_default/settings.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import os
2+
from pathlib import Path
3+
4+
BASE_DIR = Path(__file__).resolve().parent.parent
5+
6+
DEBUG = True
7+
8+
SECRET_KEY = "django-insecure-34q#9n1j46yc)+1c6&%$_$-ql%qxz^fzlf@0@1xm-zvroai1ju"
9+
10+
INSTALLED_APPS = [
11+
"django.contrib.admin",
12+
"django.contrib.auth",
13+
"django.contrib.contenttypes",
14+
"django.contrib.sessions",
15+
"django.contrib.messages",
16+
"django.contrib.staticfiles",
17+
"import_export",
18+
"import_db_default",
19+
]
20+
21+
MIDDLEWARE = [
22+
"django.middleware.security.SecurityMiddleware",
23+
"django.contrib.sessions.middleware.SessionMiddleware",
24+
"django.middleware.common.CommonMiddleware",
25+
"django.middleware.csrf.CsrfViewMiddleware",
26+
"django.contrib.auth.middleware.AuthenticationMiddleware",
27+
"django.contrib.messages.middleware.MessageMiddleware",
28+
"django.middleware.clickjacking.XFrameOptionsMiddleware",
29+
]
30+
31+
ROOT_URLCONF = "import_db_default.urls"
32+
33+
TEMPLATES = [
34+
{
35+
"BACKEND": "django.template.backends.django.DjangoTemplates",
36+
"DIRS": [],
37+
"APP_DIRS": True,
38+
"OPTIONS": {
39+
"context_processors": [
40+
"django.template.context_processors.debug",
41+
"django.template.context_processors.request",
42+
"django.contrib.auth.context_processors.auth",
43+
"django.contrib.messages.context_processors.messages",
44+
],
45+
},
46+
},
47+
]
48+
49+
WSGI_APPLICATION = "import_db_default.wsgi.application"
50+
51+
52+
# Database
53+
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
54+
55+
DATABASES = {
56+
"default": {
57+
"ENGINE": "django.db.backends.postgresql",
58+
"NAME": os.environ.get("DB_NAME", "bugs"),
59+
"USER": os.environ.get("DB_USER", "postgres"),
60+
"PASSWORD": os.environ.get("DB_PASSWORD", None),
61+
"HOST": os.environ.get("DB_HOST", None),
62+
"PORT": int(os.environ["DB_PORT"]) if "DB_PORT" in os.environ else None,
63+
},
64+
}
65+
66+
67+
# Authentication and password validation
68+
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators
69+
70+
AUTH_PASSWORD_VALIDATORS = [
71+
{
72+
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
73+
},
74+
{
75+
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
76+
},
77+
{
78+
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
79+
},
80+
{
81+
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
82+
},
83+
]
84+
85+
86+
# Internationalization
87+
# https://docs.djangoproject.com/en/5.0/topics/i18n/
88+
89+
LANGUAGE_CODE = "en"
90+
USE_I18N = True
91+
92+
TIME_ZONE = "Europe/Malta"
93+
USE_TZ = True
94+
95+
96+
# Static files (CSS, JavaScript, Images)
97+
# https://docs.djangoproject.com/en/5.0/howto/static-files/
98+
99+
STATIC_URL = "static/"
100+
MEDIA_URL = "media/"
101+
102+
STATIC_ROOT = BASE_DIR.joinpath(os.environ.get("STATIC_ROOT", "local/static")) # target directory for "collectstatic" command (used only in production)
103+
MEDIA_ROOT = BASE_DIR.joinpath(os.environ.get("MEDIA_ROOT", "local/media")) # application-related media
104+
105+
106+
# Default primary key field type
107+
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
108+
109+
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

import_db_default/urls.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.contrib import admin
2+
from django.urls import path
3+
4+
urlpatterns = [
5+
path("admin/", admin.site.urls),
6+
]

import_db_default/wsgi.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import os
2+
from django.core.wsgi import get_wsgi_application
3+
4+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "import_db_default.settings")
5+
6+
application = get_wsgi_application()

0 commit comments

Comments
 (0)