如何解决 Django 中有关外键的 DeleteView 问题?

2022-01-15 00:00:00 python django database mariadb

问题描述

我必须使用应用程序(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

相关文章