python - Django 中的 manytomany 实现(管理问题)

标签 python django postgresql admin manytomanyfield

我花了很多时间阅读关于这个主题的其他问题和 Django 文档。我对 python 比较陌生,对 django 和网页设计完全陌生。

我正在尝试设置管理站点,以便我正在从事的项目的负责人可以输入/编辑她需要的数据。 我正在尝试在两个模型之间实现多对多关系:从属隐喻和神话。

模型.py

class Mytheme(models.Model):
    myth_id = models.AutoField(primary_key=True)
    mytheme = models.CharField("Mytheme",max_length=1500, blank=True, null=True)
    superord_myth = models.CharField("Superordinate Myth", max_length=1500, blank=True, null=True)
    myth_dhuy = models.CharField("D'huy Myth",max_length=600, blank=True, null=True)
    deities_involved = models.CharField("Deities Involved",max_length=800, blank=True, null=True)
    myth = models.CharField("Myth",max_length=1000, blank=True, null=True)
    myth_comment = models.CharField("Comment",max_length=1500, blank=True, null=True)

    class Meta:
        managed = True
        db_table = 'mytheme'
        ordering = ['myth_id']
        verbose_name = 'Mytheme'

 class Subordinatemet(models.Model):

    submet_id = models.AutoField(primary_key=True)
    submetaphor = models.CharField("Subordinate Metaphor", max_length=1500, blank=True, null=True)
    sub_type = models.CharField("Type",max_length=200, blank=True, null=True)
    sub_semanticfield = models.CharField("Semantic Field",max_length=1500, blank=True, null=True) 
    sub_metanet_no = models.BigIntegerField("Metanet Number",blank=True, null=True)
    sub_metanet_frame = models.CharField("Metanet Frame",max_length=1500, blank=True, null=True)
    sub_dhuy_no = models.BigIntegerField("D'huy Number",blank=True, null=True)
    sub_dyson_ref = models.CharField("Dyson Reference",max_length=1500, blank=True, null=True)
    sub_johnlakoff_ref = models.CharField("Johnson/Lakoff Reference",max_length=1500, blank=True, null=True)
    sub_bibliography = models.CharField("Bibliography",max_length=2000, blank=True, null=True)
    sub_comment = models.CharField("Comment", max_length=2000, blank=True, null=True)
    ordinacy = models.SmallIntegerField("Ordinacy",blank=True, null=True)
    myths = models.ManyToManyField(Mytheme)

    class Meta:
        managed = True
        db_table = 'subordinatemet'
        ordering = ['submet_id']
        verbose_name = 'Subordinate Metaphor'

当我进入管理站点并尝试为 Subordinate Metaphor 添加一个新条目时,我在单击保存后被视为错误:

The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/contrib/admin/options.py", line 575, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 223, in inner
    return view(request, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/contrib/admin/options.py", line 1554, in add_view
    return self.changeform_view(request, None, form_url, extra_context)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/decorators.py", line 62, in _wrapper
    return bound_func(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/decorators.py", line 58, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/contrib/admin/options.py", line 1451, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/contrib/admin/options.py", line 1492, in _changeform_view
    self.save_related(request, form, formsets, not add)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/contrib/admin/options.py", line 1049, in save_related
    form.save_m2m()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/forms/models.py", line 439, in _save_m2m
    f.save_form_data(self.instance, cleaned_data[f.name])
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/related.py", line 1619, in save_form_data
    getattr(instance, self.attname).set(data)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 955, in set
    old_ids = set(self.using(db).values_list(self.target_field.target_field.attname, flat=True))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/query.py", line 272, in __iter__
    self._fetch_all()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/query.py", line 1179, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/query.py", line 178, in __iter__
    for row in compiler.results_iter(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1019, in results_iter
    results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch, chunk_size=chunk_size)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1068, in execute_sql
    cursor.execute(sql, params)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "subordinatemet_myths" does not exist
LINE 1: ...ECT "mytheme"."myth_id" FROM "mytheme" INNER JOIN "subordina...

我知道它正在尝试做什么,但也许我不了解幕后发生的事情足以理解它为什么要这样做。

我的问题是:

  • 这里发生了什么——系统是否在创建两个模型之间关系的“幽灵表”?

  • 如何解决此问题,以便我可以使用管理界面添加 Subordinatemet 和 Mytheme 之间的关系?

这是我的第一篇文章,所以如果有人需要更多信息,请告诉我,我会编辑我的问题。

最佳答案

Django 确实创建了一个存储多对多关系的表:

Behind the scenes, Django creates an intermediary join table to represent the many-to-many relationship.

错误信息显示该表不存在。很可能您已将字段添加到模型,但尚未将更改传播到数据库。

为此你需要:

  1. 首先使用 django-admin makemigrations 命令生成所需的迁移
  2. 使用 django-admin migrate 命令应用迁移

检查 documentation有关如何使用迁移的更多详细信息。

关于python - Django 中的 manytomany 实现(管理问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52012308/

相关文章:

python - Bootstrap 导航菜单下拉菜单不起作用

Django - 使用外键关系同时保存两条记录

python - 如何定期运行带参数的函数?

SQL - 如何根据条件插入到表中?

python - 如何使用更新查询增加 postgres 中的值(python)

python - pandas.Series 名称的作用在做差异时

python - 如何删除 python 数据表 h2oai 中的重复项

数据库列加密postgres

python - 初学者尝试调试简单的程序

python - 将结果与二次回归混淆