管理中具有 ManyToMany InvalidCursorName 的模型

Kap*_*ama 8 python django postgresql

我对 Django 和 Postres 有疑问。

Python 3.5 Django 2.2.7 psycopg2 2.8.4 postgres 9.5.19

楷模:

class Model1(models.Model):
    model1_name = models.CharField(max_length=20)


class Model2(models.Model):
    model2_name = models.CharField(max_length=20)
    model_keys = models.ManyToManyField(Model1, blank=True)
Run Code Online (Sandbox Code Playgroud)

Postgress 数据库为空:

my_base=> \d
No relations found.
Run Code Online (Sandbox Code Playgroud)

我做迁移...

python3 manage.py makemigrations
project/migrations/0004_model1_model2.py
    - Create model Model1
    - Create model Model2

python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, engine_st, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying myproject.0001_initial... OK
  Applying myproject.0002_auto_20191129_1339... OK
  Applying myproject0003_auto_20191129_1340... OK
  Applying myproject.0004_model1_model2... OK
  Applying sessions.0001_initial... OK
Run Code Online (Sandbox Code Playgroud)

...,但管理面板有错误(创建新对象的页面):

Exception Type:     InvalidCursorName
Exception Value:    cursor "_django_curs_140479737550592_1" does not exist
Run Code Online (Sandbox Code Playgroud)

有谁知道这个问题的解决方案?SQLite上不存在这样的问题。

内部服务器错误:/admin/engine_st/model2/5/change/ Traceback(最近一次调用最后一次):文件“/path/to/project/venv/lib/python3.5/site-packages/django/db/utils. py”,第 96 行,内部返回 func(*args, **kwargs) psycopg2.errors.InvalidCursorName:光标“_django_curs_139632930674432_1”不存在

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/path/to/project/venv/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1512, in cursor_iter
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File "/path/to/project/venv/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1512, in <lambda>
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File "/path/to/project/venv/lib/python3.5/site-packages/django/db/utils.py", line 96, in inner
    return func(*args, **kwargs)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/path/to/project/venv/lib/python3.5/site-packages/django/db/utils.py", line 96, in inner
    return func(*args, **kwargs)
django.db.utils.OperationalError: cursor "_django_curs_139632930674432_1" does not exist


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/path/to/project/venv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 145, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 143, in _get_response
    response = response.render()
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/response.py", line 106, in render
    self.content = self.rendered_content
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/response.py", line 83, in rendered_content
    content = template.render(context, self._request)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 171, in render
    return self._render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/loader_tags.py", line 188, in render
    return template.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 173, in render
    return self._render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/defaulttags.py", line 309, in render
    return nodelist.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/defaulttags.py", line 309, in render
    return nodelist.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 993, in render
    return render_value_in_context(output, context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 972, in render_value_in_context
    value = str(value)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/utils/html.py", line 388, in <lambda>
    klass.__str__ = lambda self: mark_safe(klass_str(self))
  File "/path/to/project/venv/lib/python3.5/site-packages/django/forms/boundfield.py", line 33, in __str__
    return self.as_widget()
  File "/path/to/project/venv/lib/python3.5/site-packages/django/forms/boundfield.py", line 93, in as_widget
    renderer=self.form.renderer,
  File "/path/to/project/venv/lib/python3.5/site-packages/django/forms/widgets.py", line 241, in render
    context = self.get_context(name, value, attrs)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/contrib/admin/widgets.py", line 288, in get_context
    'rendered_widget': self.widget.render(name, value, attrs),
  File "/path/to/project/venv/lib/python3.5/site-packages/django/forms/widgets.py", line 241, in render
    context = self.get_context(name, value, attrs)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/forms/widgets.py", line 680, in get_context
    context = super().get_context(name, value, attrs)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/forms/widgets.py", line 641, in get_context
    context['widget']['optgroups'] = self.optgroups(name, context['widget']['value'], attrs)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/forms/widgets.py", line 589, in optgroups
    for index, (option_value, option_label) in enumerate(self.choices):
  File "/path/to/project/venv/lib/python3.5/site-packages/django/forms/models.py", line 1137, in __iter__
    for obj in queryset:
  File "/path/to/project/venv/lib/python3.5/site-packages/django/db/models/query.py", line 341, in _iterator
    yield from self._iterable_class(self, chunked_fetch=use_chunked_fetch, chunk_size=chunk_size)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/db/models/query.py", line 72, in __iter__
    for row in compiler.results_iter(results):
  File "/path/to/project/venv/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1515, in cursor_iter
    cursor.close()
psycopg2.errors.InvalidCursorName: cursor "_django_curs_139632930674432_1" does not exist
Run Code Online (Sandbox Code Playgroud)

小智 5

python manage.py migrate --run-syncdb
Run Code Online (Sandbox Code Playgroud)

  • 请不要仅发布代码作为答案,还要提供解释您的代码的作用以及它如何解决问题的问题。带解释的答案通常更有帮助,质量也更好,并且更有可能吸引点赞。 (10认同)