Ive been trying to save a json file created from a to a filefield, I got an unsuported operation error "not readable", this is my code
from django.core.files.base import File
#receiver(post_save,sender=ProyArq)
def ifc_a_json(sender,instance,*args,**kwargs):
if instance.arch_ifc:
jsoon = ifc_2_json("path_to_file")
json_file_nom = instance.nombre.replace(' ','')+'.json'
with open(json_file_nom, 'w') as outfile:
json.dump(jsoon, outfile, indent=2)
json_fk = JsonIFC.objects.create(proy_fk=instance)
json_fk.ifc_json.save(json_file_nom,File(outfile),True)
im working with IFC files, and I want to store them also as json, I tried instead of saving the json as a JSONField, to save it as a foreign key file since the size of the json Im working are above 10mb, is this the best approach for this??
error details
Exception Type: UnsupportedOperation
Exception Value: not readable
Exception Location: /home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/core/files/base.py in chunks, line 60
Traceback (most recent call last):
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/contrib/admin/options.py", line 607, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/contrib/admin/sites.py", line 231, in inner
return view(request, *args, **kwargs)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1641, in change_view
return self.changeform_view(request, object_id, form_url, extra_context)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1522, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1565, in _changeform_view
self.save_model(request, new_object, form, not add)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1081, in save_model
obj.save()
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/db/models/base.py", line 746, in save
force_update=force_update, update_fields=update_fields)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/db/models/base.py", line 795, in save_base
update_fields=update_fields, raw=raw, using=using,
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 175, in send
for receiver in self._live_receivers(sender)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 175, in <listcomp>
for receiver in self._live_receivers(sender)
File "/home/fcr/anaconda3/envs/gda/a_viurb/b_proy/models.py", line 105, in ifc_a_json
json_fk.ifc_json.save(json_file_nom,File(outfile),True)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/db/models/fields/files.py", line 87, in save
self.name = self.storage.save(name, content, max_length=self.field.max_length)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/core/files/storage.py", line 52, in save
return self._save(name, content)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/core/files/storage.py", line 271, in _save
for chunk in content.chunks():
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/core/files/base.py", line 60, in chunks
data = self.read(chunk_size)
io.UnsupportedOperation: not readable
I solved the error, I needed to change the file open from "w" to "a+", which allows reading and writing
Related
The problem
I'm trying to send a Djoser user's activation email using django-mailer.
However, I receive the following error:
TL;DR:
TypeError: cannot pickle '_io.BufferedReader' object
FULL:
Traceback (most recent call last):
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/rest_framework/viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/rest_framework/mixins.py", line 19, in create
self.perform_create(serializer)
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/djoser/views.py", line 144, in perform_create
settings.EMAIL.activation(self.request, context).send(to)
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/templated_mail/mail.py", line 78, in send
super(BaseEmailMessage, self).send(*args, **kwargs)
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/django/core/mail/message.py", line 298, in send
return self.get_connection(fail_silently).send_messages([self])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/mailer/backend.py", line 15, in send_messages
messages = Message.objects.bulk_create([
^
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/mailer/backend.py", line 16, in <listcomp>
Message(email=email) for email in email_messages
^^^^^^^^^^^^^^^^^^^^
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/django/db/models/base.py", line 554, in __init__
_setattr(self, prop, value)
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/mailer/models.py", line 150, in _set_email
self.message_data = email_to_db(val)
^^^^^^^^^^^^^^^^
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/mailer/models.py", line 94, in email_to_db
return base64_encode(pickle.dumps(email)).decode('ascii')
^^^^^^^^^^^^^^^^^^^
TypeError: cannot pickle '_io.BufferedReader' object
Internal Server Error: /api/auth/users/
Traceback (most recent call last):
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/rest_framework/viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/rest_framework/mixins.py", line 19, in create
self.perform_create(serializer)
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/djoser/views.py", line 144, in perform_create
settings.EMAIL.activation(self.request, context).send(to)
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/templated_mail/mail.py", line 78, in send
super(BaseEmailMessage, self).send(*args, **kwargs)
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/django/core/mail/message.py", line 298, in send
return self.get_connection(fail_silently).send_messages([self])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/mailer/backend.py", line 15, in send_messages
messages = Message.objects.bulk_create([
^
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/mailer/backend.py", line 16, in <listcomp>
Message(email=email) for email in email_messages
^^^^^^^^^^^^^^^^^^^^
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/django/db/models/base.py", line 554, in __init__
_setattr(self, prop, value)
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/mailer/models.py", line 150, in _set_email
self.message_data = email_to_db(val)
^^^^^^^^^^^^^^^^
File "/Users/joaoalbuquerque/pyenv/versions/3.11.0/envs/garvy/lib/python3.11/site-packages/mailer/models.py", line 94, in email_to_db
return base64_encode(pickle.dumps(email)).decode('ascii')
^^^^^^^^^^^^^^^^^^^
TypeError: cannot pickle '_io.BufferedReader' object
Libraries and versions used (Python 3.11):
"djangorestframework==3.14.0",
"dj-database-url==1.0.0",
"psycopg2-binary==2.9.5",
"django-cors-headers==3.13.0",
"gunicorn==20.1.0",
"pyjwt==2.6.0",
"python-dotenv==0.21.0",
"djoser==2.1.0",
"djangorestframework-simplejwt==4.8.0",
"django-mailer==2.1",
What I've already tried:
I found this related issue on Djoser's Github. However resolution support with django-mailer in version 2.1 didn't work around here.
Related issue: https://github.com/sunscrapers/djoser/issues/609
when clicking publish or save draft I got this error
wagtail.core.models.Page.DoesNotExist: Page matching query does not exist.
this happens only with old pages, the newly created pages are being able to be created and saved without any errors
NB: the new pages do not have children pages yet
trying to find out what is causing this error despite I did not override the save method
any suggestion or hint will be very helpful thank you
here the traceback:
wagtail.core.models.DoesNotExist
wagtail.core.models.Page.DoesNotExist: Page matching query does not exist.
Traceback (most recent call last)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/contrib/staticfiles/handlers.py", line 76, in __call__
return self.application(environ, start_response)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/core/handlers/wsgi.py", line 133, in __call__
response = self.get_response(request)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/core/handlers/base.py", line 130, in get_response
response = self._middleware_chain(request)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/core/handlers/exception.py", line 49, in inner
response = response_for_exception(request, exc)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/core/handlers/exception.py", line 103, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/core/handlers/exception.py", line 138, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django_extensions/management/technical_response.py", line 37, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/six.py", line 702, in reraise
raise value.with_traceback(tb)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/wagtail/admin/urls/__init__.py", line 127, in wrapper
return view_func(request, *args, **kwargs)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/wagtail/admin/auth.py", line 172, in decorated_view
response = view_func(request, *args, **kwargs)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/wagtail/admin/views/pages/edit.py", line 131, in dispatch
return super().dispatch(request)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/views/generic/base.py", line 98, in dispatch
return handler(request, *args, **kwargs)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/wagtail/admin/views/pages/edit.py", line 218, in post
return self.form_valid(self.form)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/wagtail/admin/views/pages/edit.py", line 238, in form_valid
return self.publish_action()
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/wagtail/admin/views/pages/edit.py", line 286, in publish_action
revision.publish(
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/wagtail/core/models.py", line 2972, in publish
page.save()
File "/home/oladhari/gounite-v2/GOunite/v2/home/models.py", line 98, in save
super().save(*args, **kwargs)
File "/usr/lib/python3.8/contextlib.py", line 75, in inner
return func(*args, **kwds)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/wagtail/core/models.py", line 1028, in save
result = super().save(**kwargs)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/modelcluster/models.py", line 199, in save
getattr(self, relation).commit()
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/modelcluster/fields.py", line 202, in commit
item.delete()
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/wagtail/core/models.py", line 1094, in delete
return Page.objects.get(id=self.id).delete(*args, **kwargs)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/db/models/query.py", line 429, in get
raise self.model.DoesNotExist(
wagtail.core.models.Page.DoesNotExist: Page matching query does not exist.
The part that is throwing errors looks like code in your models that relates one page to other pages. See this line: return Page.objects.get(id=self.id).delete(*args, **kwargs)? So I think we need to know what "items" are and how the page you are saving uses them.
Please post the models for the page you are saving and the pages that are referenced as 'items'
Finally resolved it
we should not use ParentalKey with a Page model (we were using it in GOPart model which is a Page model, changing it to ForeignKey resolved the error and now we can save the HomePage
class MyModel(RoutablePageMixin, Page, Orderable):
parent_page_types: list = []
company = ParentalKey(
"home.HomePage",
on_delete=models.SET_NULL,
verbose_name=_("Company"),
null=True,
blank=False,
)
changing it to:
class MyModel(RoutablePageMixin, Page):
parent_page_types: list = []
company = models.ForeignKey(
"home.HomePage",
on_delete=models.SET_NULL,
verbose_name=_("Company"),
null=True,
blank=False,
)
a Page can not be Orderable
Solution:- I had deleted my migration .py files which caused this problem. Creating a new app with the same code works now.
I am not sure why i get a varying(20) error even when the max_length I've set is 200 for a CharField
I looked at all the other posts and all of them had an issue with the length of the value.
However, even if i enter "abc" in the name field it gives me this error. Any help is appreciated. Thanks!
Django Version: 3.1.5
Python Version: 3.8.5
from django.db import models
from django.contrib.gis.db import models
from django.db.models import Manager as GeoManager
# Create your models here.
class Incidences(models.Model):
name = models.CharField(max_length=200)
location = models.PointField(srid=4326)
objects = GeoManager()
def _str_(self):
return self.name
Traceback (most recent call last):
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
The above exception (value too long for type character varying(20)
) was the direct cause of the following exception:
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\contrib\admin\options.py", line 614, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\contrib\admin\sites.py", line 233, in inner
return view(request, *args, **kwargs)
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\contrib\admin\options.py", line 1653, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\contrib\admin\options.py", line 1534, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\contrib\admin\options.py", line 1580, in _changeform_view
self.save_model(request, new_object, form, not add)
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\contrib\admin\options.py", line 1093, in save_model
obj.save()
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\db\models\base.py", line 753, in save
self.save_base(using=using, force_insert=force_insert,
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\db\models\base.py", line 790, in save_base
updated = self._save_table(
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\db\models\base.py", line 895, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\db\models\base.py", line 933, in _do_insert
return manager._insert(
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\db\models\query.py", line 1254, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\db\models\sql\compiler.py", line 1397, in execute_sql
cursor.execute(sql, params)
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\db\backends\utils.py", line 98, in execute
return super().execute(sql, params)
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\anuj\geo_django_tutorial\tutorial_geodjango\lifeingis\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
Exception Type: DataError at /admin/reporter/incidences/add/
Exception Value: value too long for type character varying(20)
I am working with a custom build user model in Django and there was some message in the terminal at the time of migrate given in the stackoverflow link.
However, I have failed to solve that and it didn't hamper to run the project, so I actually ignore it and continue with my code. Now when I try to log-in to the admin panel(http://127.0.0.1:8000/admin/) it shows the following error:
Internal Server Error: /admin/login/
Traceback (most recent call last):
File "G:\Python\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "G:\Python\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "G:\Python\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "G:\Python\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "G:\Python\lib\site-packages\django\contrib\admin\sites.py", line 407, in login
return LoginView.as_view(**defaults)(request)
File "G:\Python\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "G:\Python\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "G:\Python\lib\site-packages\django\views\decorators\debug.py", line 76, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "G:\Python\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "G:\Python\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "G:\Python\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "G:\Python\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "G:\Python\lib\site-packages\django\contrib\auth\views.py", line 63, in dispatch
return super().dispatch(request, *args, **kwargs)
File "G:\Python\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "G:\Python\lib\site-packages\django\views\generic\edit.py", line 142, in post
return self.form_valid(form)
File "G:\Python\lib\site-packages\django\contrib\auth\views.py", line 92, in form_valid
auth_login(self.request, form.get_user())
File "G:\Python\lib\site-packages\django\contrib\auth\__init__.py", line 131, in login
user_logged_in.send(sender=user.__class__, request=request, user=user)
File "G:\Python\lib\site-packages\django\dispatch\dispatcher.py", line 173, in send
return [
File "G:\Python\lib\site-packages\django\dispatch\dispatcher.py", line 174, in <listcomp>
(receiver, receiver(signal=self, sender=sender, **named))
File "G:\Python\lib\site-packages\django\contrib\auth\models.py", line 20, in update_last_login
user.save(update_fields=['last_login'])
File "G:\Python\lib\site-packages\django\contrib\auth\base_user.py", line 66, in save
super().save(*args, **kwargs)
File "G:\Python\lib\site-packages\django\db\models\base.py", line 745, in save
self.save_base(using=using, force_insert=force_insert,
File "G:\Python\lib\site-packages\django\db\models\base.py", line 782, in save_base
updated = self._save_table(
File "G:\Python\lib\site-packages\django\db\models\base.py", line 847, in _save_table
raise ValueError("Cannot force an update in save() with no primary key.")
ValueError: Cannot force an update in save() with no primary key.
I was facing the same issue,I searched everywhere.
I found the answer in the link I will attach below.
1-to change the database name.
2-makemigrations and migrate.
but it failed to work until I read towen's comment! I was always creating
the super user before
migrating, which is wrong!
you have make the migrations before creating the super user.
so
python manage.py makemigrations
then
python manage.py migrate
then all the tables are ready to work properly, now we can create the super user, and it will have the primary key and it will work
python manage.py createsuperuser
https://github.com/nesdis/djongo/issues/3
I'm making a simple booking app in Django. I've made a model where I'll store booking data for each day of the week and tried to add a table based on it on admin page. But when I click "save", I receive an error:
Traceback (most recent call last):
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 413, in execute
return Database.Cursor.execute(self, query, params)
The above exception (no such table: baseapp_bookingmodel) was the direct cause of the following exception:
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\contrib\admin\options.py", line 614, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\contrib\admin\sites.py", line 233, in inner
return view(request, *args, **kwargs)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\contrib\admin\options.py", line 1653, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\contrib\admin\options.py", line 1534, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\contrib\admin\options.py", line 1580, in _changeform_view
self.save_model(request, new_object, form, not add)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\contrib\admin\options.py", line 1093, in save_model
obj.save()
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\db\models\base.py", line 754, in save
force_update=force_update, update_fields=update_fields)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\db\models\base.py", line 792, in save_base
force_update, using, update_fields,
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\db\models\base.py", line 895, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\db\models\base.py", line 935, in _do_insert
using=using, raw=raw,
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\db\models\query.py", line 1249, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1397, in execute_sql
cursor.execute(sql, params)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\db\backends\utils.py", line 98, in execute
return super().execute(sql, params)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\katja\PycharmProjects\django\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 413, in execute
return Database.Cursor.execute(self, query, params)
Exception Type: OperationalError at /admin/baseapp/bookingmodel/add/
Exception Value: no such table: baseapp_bookingmodel
Any ideas why this happens? Earlier I received the same error when I tried to migrate some changes in the model, but it was fixed by deleting the database and migrations and making them again. I also get the same error when I try to reference the model on pages.
models.py:
from django.db import models
import sys
sys.path.append('C:/Users/katja/PycharmProjects/django/taskmanager')
def empty_data():
return [[0 for i in range(7)] for j in range(5)]
def empty_title():
return 'Monday'
class BookingModel(models.Model):
title = models.CharField(max_length=16, default=empty_title())
data = models.TextField(default=empty_data())
def __str__(self):
return self.title
class Meta:
verbose_name = 'Booking'
verbose_name_plural = 'Booking'
You should do migrations:
python manage.py makemigrations
python manage.py migrate
Your database has not table for this model.