不同 Django 应用程序中不同模型的相同名称

The same name for different models in different Django applications

我在 Django 项目中有应用程序 1 和应用程序 2。我需要每个应用程序都有自己的 table 命名任务和文件。并且每个应用程序中的文件 table 将 ForeignKey 引用到其自己应用程序中的任务 table。它看起来像这样:

app1.models

class Task(BasicTask):
    task_id = models.IntegerField(primary_key=True)

class File(BasicFile):
    file_id = models.AutoField(primary_key=True)
    task = models.ForeignKey(Task, on_delete=models.CASCADE, related_name='Task_one')

app2.models

class Task(BasicTask):
    task_id = models.IntegerField(primary_key=True)

class File(BasicFile):
    file_id = models.AutoField(primary_key=True)
    task = models.ForeignKey(Task, on_delete=models.CASCADE, related_name='Task_two')

main.models

class BasicTask(models.Model):
    name = models.TextField()
    priority = models.IntegerField()
    status = models.IntegerField(default=0)
    user = models.ForeignKey(
        CustomUser, on_delete=models.CASCADE, null=True, related_name='+')
    
    def __str__(self):
        return f"{self.id}. {self.name}"


class BasicFile(models.Model):
    unique_id = models.TextField()
    status = models.IntegerField(default=0)
    user = models.ForeignKey(
        CustomUser, on_delete=models.CASCADE, null=True, related_name='+')
    content = models.TextField()
    def __str__(self):
        return f"{self.id}. {self.unique_id}"
        

问题是我在进行迁移时遇到以下错误:

ERRORS:
app2.Task.basictask_ptr: (fields.E304) Reverse accessor for 'app2.Task.basictask_ptr' clashes with reverse accessor for 'app1.Task.basictask_ptr'.
        HINT: Add or change a related_name argument to the definition for 'app2.Task.basictask_ptr' or 'app1.Task.basictask_ptr'.
app2.Task.basictask_ptr: (fields.E305) Reverse query name for 'app2.Task.basictask_ptr' clashes with reverse query name for 'app1.Task.basictask_ptr'.
        HINT: Add or change a related_name argument to the definition for 'app2.Task.basictask_ptr' or 'app1.Task.basictask_ptr'.
app1.Task.basictask_ptr: (fields.E304) Reverse accessor for 'app1.Task.basictask_ptr' clashes with reverse accessor for 'app2.Task.basictask_ptr'.
        HINT: Add or change a related_name argument to the definition for 'app1.Task.basictask_ptr' or 'app2.Task.basictask_ptr'.
app1.Task.basictask_ptr: (fields.E305) Reverse query name for 'app1.Task.basictask_ptr' clashes with reverse query name for 'app2.Task.basictask_ptr'.
        HINT: Add or change a related_name argument to the definition for 'app1.Task.basictask_ptr' or 'app2.Task.basictask_ptr'.

你能告诉我如何更正这些错误吗?

Django 向您显示错误并说:

Add or change a related_name argument to the definition for ...

所以你需要去 models.fields:

  1. app2.Task.basictask_ptr
  2. app1.Task.basictask_ptr

和 add/change related_name 两个模型的字段 basictask_ptr 为不同的值。

有关相关领域的更多信息是 here

因此,为了修复此错误,有必要这样做:

app1.models

class Task(BasicTask):
    task_id = models.IntegerField(primary_key=True)
    basictask_ptr = models.OneToOneField(BasicTask, on_delete=models.CASCADE, related_name='app1', parent_link=True)

class File(BasicFile):
    file_id = models.AutoField(primary_key=True)
    basicfile_ptr = models.OneToOneField(BasicFile, on_delete=models.CASCADE, related_name='app1', parent_link=True)
    task = models.ForeignKey(Task, on_delete=models.CASCADE)

app2.models

class Task(BasicTask):
    task_id = models.IntegerField(primary_key=True)
    basictask_ptr = models.OneToOneField(BasicTask, on_delete=models.CASCADE, related_name='app2', parent_link=True)

class File(BasicFile):
    file_id = models.AutoField(primary_key=True)
    basicfile_ptr = models.OneToOneField(BasicFile, on_delete=models.CASCADE, related_name='app2', parent_link=True)
    task = models.ForeignKey(Task, on_delete=models.CASCADE)

main.models

class BasicTask(models.Model):
    name = models.TextField()
    priority = models.IntegerField()
    status = models.IntegerField(default=0)
    user = models.ForeignKey(
        CustomUser, on_delete=models.CASCADE, null=True, related_name='+')
    
    def __str__(self):
        return f"{self.id}. {self.name}"


class BasicFile(models.Model):
    unique_id = models.TextField()
    status = models.IntegerField(default=0)
    user = models.ForeignKey(
        CustomUser, on_delete=models.CASCADE, null=True, related_name='+')
    content = models.TextField()
    def __str__(self):
        return f"{self.id}. {self.unique_id}"