How can solve a DeleteView problem in Django concerning a foreign key?(如何解决 Django 中有关外键的 DeleteView 问题?)
问题描述
我必须使用应用程序(DMS 和 ObjPDW).第一个是用于管理一些文件.在这我有一个模型 DMS_Dokument,其中包括一个 FileField 等等.最近我为后一个应用程序(ObjPDW)添加了一个新模型,并在 Dokument_DMS 中添加了一个外键:
I have to apps (DMS and ObjPDW). The first one is for managing some files. In this I have a model DMS_Dokument, which includes a FileField and some more. Recently I added a new model to the latter app (ObjPDW) and I included a foreign key to Dokument_DMS:
class Zahlungsstrom(models.Model):
zahlung_bezeichnung = models.CharField(max_length=550, blank=False, null=False, verbose_name="Bezeichnung")
zahlung_betrag = models.DecimalField(max_digits=7, decimal_places=2, default=None, blank=True, null=True)
zahlung_dok_fk = models.ForeignKey(dmsdok.DMS_Dokument, on_delete=models.SET_DEFAULT, default=None, null=True, blank=True, verbose_name="Zahlungsdokument")
现在我想删除一个 DMS_Dokument 对象(使用 DeleteView CBV),但它给了我一个prorammingerror":(1146,表'DB_DMS.ObjPDW_zahlungsstrom'不存在")
Now I wanted to delete a DMS_Dokument object (using the DeleteView CBV), but it gives me a "prorammingerror": "(1146, "Table 'DB_DMS.ObjPDW_zahlungsstrom' doesn't exist")"
我不知道问题是什么.:(
I have no clue what the problem is. :(
只是为了明确这一点.这两个应用程序都有自己的数据库.我知道 Django 不建议在两个数据库之间关联模型,但由于我不是经验丰富的程序员,我不知道为什么我可以使关系工作,但删除是个问题.
Just to be clear on this. Both apps have their own databases. I know that Django is not recommending to relate models between two databases, but as I am no experienced programmer I do not know why I can make the relations work, but deletion is such a problem.
此外,我想在此处包含更多代码,这是关于 DMS_Dokument 模型的.它还有一个删除定义.
Furthermore I want to include some more code here, which is about the DMS_Dokument model. It also has a delete def.
class DMS_Dokument(models.Model):
dms_dok_titel = models.CharField(max_length=255, blank=True)
dms_dok_beschreibung = models.CharField(max_length=3000, blank=True, null=True)
dms_dok_datei = models.FileField(max_length=255,upload_to='DMS/')
dms_dok_gehoert_zu_app = models.CharField(max_length=255, choices=app_choices, blank=False, null=False)
def save(self, *args, **kwargs):
preserve_ext = extension(self.dms_dok_datei.name)
neuer_dateiname = self.dms_dok_gehoert_zu_app + '_' + self.dms_dok_titel + '_' + self.dms_dok_hochgeladen_am.strftime("%d.%m.%Y")
self.dms_dok_datei.name = neuer_dateiname + preserve_ext
super(DMS_Dokument, self).save(*args, **kwargs)
def delete(self):
self.indexes.all().delete()
super(DMS_Dokument, self).delete()
也许这会有所帮助.
推荐答案
报错说表不存在.您必须发出以下命令来创建迁移并在您的数据库中创建表:
The error says the table does not exist. You must issue the following commands to create the migrations and make the table in you DB:
python manage.py makemigrations
python manage.py migrate
这篇关于如何解决 Django 中有关外键的 DeleteView 问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何解决 Django 中有关外键的 DeleteView 问题?


基础教程推荐
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01