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



我在Django项目中有应用程序1和应用程序2。我需要每个应用程序都有自己的名为任务和文件的表。每个应用程序中的File表将ForeignKey引用到自己应用程序中的Task表。

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

并将两个模型的basictask_ptr字段的related_name添加/更改为不同的值。

更多相关字段的信息在这里

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

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}"

相关内容

  • 没有找到相关文章

最新更新