Django Error 'bytes' object has no attribute '_committed'
Проблема следующая - пытаюсь добавить фото из папки в БД(SQLite). Выползает ошибка 'bytes' object has no attribute '_committed'. Прошу помощи, так как перелазил уже все. Фрагменты кода:
models.py
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=255, verbose_name='Название')
text = models.TextField(verbose_name='Описание')
photo = models.FileField(upload_to="photos/%Y/%m/%d/", verbose_name='Фото', blank=True)
date = models.DateField(verbose_name='Дата')
parse.py(фрагмент)
from .models import Post
def getPostPata():
def readImage(file_name, new_post):
with open(file_name, "rb") as file:
img = file.read()
new_post.photo = img
new_post.save()
new_post = Post(title='title', text='text', date='date')
readImage('site/media/photo.jpg', new_post)
views.py
from django.views.generic import ListView
from .models import *
from .parse import get_post_data
class MainPage(ListView):
model = Post
template_name = 'site/main.html'
context_object_name = 'posts'
def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(**kwargs)
return context
get_post_data()
Полный лог:
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\threading.py", line 954, in _bootstrap_inner
self.run()
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "D:\Python\TZ_NASA\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "D:\Python\TZ_NASA\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "D:\Python\TZ_NASA\lib\site-packages\django\core\management\base.py", line 419, in check
all_issues = checks.run_checks(
File "D:\Python\TZ_NASA\lib\site-packages\django\core\checks\registry.py", line 76, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "D:\Python\TZ_NASA\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "D:\Python\TZ_NASA\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "D:\Python\TZ_NASA\lib\site-packages\django\urls\resolvers.py", line 412, in check
for pattern in self.url_patterns:
File "D:\Python\TZ_NASA\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\Python\TZ_NASA\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "D:\Python\TZ_NASA\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\Python\TZ_NASA\lib\site-packages\django\urls\resolvers.py", line 591, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "D:\Python\Django\TZ_NASA\nasa_site\nasa_site\urls.py", line 8, in <module>
path('', include('nasa.urls')),
File "D:\Python\TZ_NASA\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "D:\Python\Django\TZ_NASA\nasa_site\nasa\urls.py", line 3, in <module>
from .views import *
File "D:\Python\Django\TZ_NASA\nasa_site\nasa\views.py", line 7, in <module>
class MainPage(ListView):
File "D:\Python\Django\TZ_NASA\nasa_site\nasa\views.py", line 17, in MainPage
get_post_data()
File "D:\Python\Django\TZ_NASA\nasa_site\nasa\parse.py", line 50, in get_post_data
readImage(f'nasa/media/{date_out}.jpg', new_post)
File "D:\Python\Django\TZ_NASA\nasa_site\nasa\parse.py", line 46, in readImage
new_post.save()
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\base.py", line 726, in save
self.save_base(using=using, force_insert=force_insert,
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\base.py", line 763, in save_base
updated = self._save_table(
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\base.py", line 868, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\base.py", line 906, in _do_insert
return manager._insert(
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\query.py", line 1270, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\sql\compiler.py", line 1415, in execute_sql
for sql, params in self.as_sql():
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\sql\compiler.py", line 1358, in as_sql
value_rows = [
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\sql\compiler.py", line 1359, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\sql\compiler.py", line 1359, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\sql\compiler.py", line 1310, in pre_save_val
return field.pre_save(obj, add=True)
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\fields\files.py", line 300, in pre_save
if file and not file._committed:
AttributeError: '_io.BufferedReader' object has no attribute '_committed'
D:\Python\Django\TZ_NASA\nasa_site\nasa\parse.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\threading.py", line 954, in _bootstrap_inner
self.run()
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "D:\Python\TZ_NASA\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "D:\Python\TZ_NASA\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "D:\Python\TZ_NASA\lib\site-packages\django\core\management\base.py", line 419, in check
all_issues = checks.run_checks(
File "D:\Python\TZ_NASA\lib\site-packages\django\core\checks\registry.py", line 76, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "D:\Python\TZ_NASA\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "D:\Python\TZ_NASA\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "D:\Python\TZ_NASA\lib\site-packages\django\urls\resolvers.py", line 412, in check
for pattern in self.url_patterns:
File "D:\Python\TZ_NASA\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\Python\TZ_NASA\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "D:\Python\TZ_NASA\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\Python\TZ_NASA\lib\site-packages\django\urls\resolvers.py", line 591, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "D:\Python\Django\TZ_NASA\nasa_site\nasa_site\urls.py", line 8, in <module>
path('', include('nasa.urls')),
File "D:\Python\TZ_NASA\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "D:\Python\Django\TZ_NASA\nasa_site\nasa\urls.py", line 3, in <module>
from .views import *
File "D:\Python\Django\TZ_NASA\nasa_site\nasa\views.py", line 7, in <module>
class MainPage(ListView):
File "D:\Python\Django\TZ_NASA\nasa_site\nasa\views.py", line 17, in MainPage
get_post_data()
File "D:\Python\Django\TZ_NASA\nasa_site\nasa\parse.py", line 50, in get_post_data
readImage(f'nasa/media/{date_out}.jpg', new_post)
File "D:\Python\Django\TZ_NASA\nasa_site\nasa\parse.py", line 46, in readImage
new_post.save()
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\base.py", line 726, in save
self.save_base(using=using, force_insert=force_insert,
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\base.py", line 763, in save_base
updated = self._save_table(
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\base.py", line 868, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\base.py", line 906, in _do_insert
return manager._insert(
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\query.py", line 1270, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\sql\compiler.py", line 1415, in execute_sql
for sql, params in self.as_sql():
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\sql\compiler.py", line 1358, in as_sql
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\sql\compiler.py", line 1359, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\sql\compiler.py", line 1359, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\sql\compiler.py", line 1310, in pre_save_val
return field.pre_save(obj, add=True)
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\fields\files.py", line 300, in pre_save
if file and not file._committed:
AttributeError: 'list' object has no attribute '_committed'
PS D:\Python\Django\TZ_NASA\nasa_site> python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\threading.py", line 954, in _bootstrap_inner
self.run()
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "D:\Python\TZ_NASA\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "D:\Python\TZ_NASA\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "D:\Python\TZ_NASA\lib\site-packages\django\core\management\base.py", line 419, in check
all_issues = checks.run_checks(
File "D:\Python\TZ_NASA\lib\site-packages\django\core\checks\registry.py", line 76, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "D:\Python\TZ_NASA\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "D:\Python\TZ_NASA\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "D:\Python\TZ_NASA\lib\site-packages\django\urls\resolvers.py", line 412, in check
for pattern in self.url_patterns:
File "D:\Python\TZ_NASA\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\Python\TZ_NASA\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "D:\Python\TZ_NASA\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\Python\TZ_NASA\lib\site-packages\django\urls\resolvers.py", line 591, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "D:\Python\Django\TZ_NASA\nasa_site\nasa_site\urls.py", line 8, in <module>
path('', include('nasa.urls')),
File "D:\Python\TZ_NASA\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "D:\Python\Django\TZ_NASA\nasa_site\nasa\urls.py", line 3, in <module>
from .views import *
File "D:\Python\Django\TZ_NASA\nasa_site\nasa\views.py", line 7, in <module>
class MainPage(ListView):
File "D:\Python\Django\TZ_NASA\nasa_site\nasa\views.py", line 17, in MainPage
get_post_data()
File "D:\Python\Django\TZ_NASA\nasa_site\nasa\parse.py", line 50, in get_post_data
readImage(f'nasa/media/{date_out}.jpg', new_post)
File "D:\Python\Django\TZ_NASA\nasa_site\nasa\parse.py", line 46, in readImage
new_post.save()
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\base.py", line 726, in save
self.save_base(using=using, force_insert=force_insert,
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\base.py", line 763, in save_base
updated = self._save_table(
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\base.py", line 868, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\base.py", line 906, in _do_insert
return manager._insert(
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\query.py", line 1270, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\sql\compiler.py", line 1415, in execute_sql
for sql, params in self.as_sql():
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\sql\compiler.py", line 1358, in as_sql
value_rows = [
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\sql\compiler.py", line 1359, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\sql\compiler.py", line 1359, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\sql\compiler.py", line 1310, in pre_save_val
return field.pre_save(obj, add=True)
File "D:\Python\TZ_NASA\lib\site-packages\django\db\models\fields\files.py", line 300, in pre_save
if file and not file._committed:
AttributeError: 'bytes' object has no attribute '_committed'