Django 中的 manytomany 实现(管理问题)
manytomany Implementation in Django (admin issue)
我花了很多时间阅读关于这个主题的其他问题和 Django 文档。我是 python 的新手,也是 django 和 webdesign 的新手。
我正在尝试设置管理站点,以便我正在从事的项目的负责人可以 enter/edit 她需要的数据。
我正在尝试在两个模型之间实现多对多关系:从属隐喻和神话。
models.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...
我明白它试图做什么,但也许我不太了解幕后发生的事情,无法理解 - 为什么 - 它正在尝试这样做。
我的问题是:
这里发生了什么 -- 系统是否在两个模型之间创建 "ghost table" 关系?
如何解决这个问题,以便我可以使用管理界面添加 Subordinatemet 和 Mytheme 之间的关系?
这是我的第一个 post,所以如果有人需要更多信息,请告诉我,我会编辑我的问题。
Django 确实创建了一个存储多对多关系的 table:
Behind the scenes, Django creates an intermediary join table to
represent the many-to-many relationship.
错误消息显示 table 不存在。很可能您已将该字段添加到模型,但尚未将更改传播到数据库。
为此你需要:
- 首先使用
django-admin makemigrations
命令生成所需的迁移
- 使用
django-admin migrate
命令应用迁移
查看 documentation 了解有关如何使用迁移的更多详细信息。
我花了很多时间阅读关于这个主题的其他问题和 Django 文档。我是 python 的新手,也是 django 和 webdesign 的新手。
我正在尝试设置管理站点,以便我正在从事的项目的负责人可以 enter/edit 她需要的数据。 我正在尝试在两个模型之间实现多对多关系:从属隐喻和神话。
models.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...
我明白它试图做什么,但也许我不太了解幕后发生的事情,无法理解 - 为什么 - 它正在尝试这样做。
我的问题是:
这里发生了什么 -- 系统是否在两个模型之间创建 "ghost table" 关系?
如何解决这个问题,以便我可以使用管理界面添加 Subordinatemet 和 Mytheme 之间的关系?
这是我的第一个 post,所以如果有人需要更多信息,请告诉我,我会编辑我的问题。
Django 确实创建了一个存储多对多关系的 table:
Behind the scenes, Django creates an intermediary join table to represent the many-to-many relationship.
错误消息显示 table 不存在。很可能您已将该字段添加到模型,但尚未将更改传播到数据库。
为此你需要:
- 首先使用
django-admin makemigrations
命令生成所需的迁移 - 使用
django-admin migrate
命令应用迁移
查看 documentation 了解有关如何使用迁移的更多详细信息。