我试图将多个作业分配给发票/购买,但是我遇到了问题。我错过了什么吗?这需要是多对多还是什么?
我收到以下错误:
invoices.Job.job: (fields.E304) Reverse accessor for 'Job.job' clashes with reverse accessor for 'Job.job'.
HINT: Add or change a related_name argument to the definition for 'Job.job' or 'Job.job'.
purchases.Job.job: (fields.E304) Reverse accessor for 'Job.job' clashes with reverse accessor for 'Job.job'.
HINT: Add or change a related_name argument to the definition for 'Job.job' or 'Job.job'.
购买/ models.py
class Purchase(models.Model):
...
class Job(models.Model):
purchase = models.ForeignKey(Purchase)
job = models.ForeignKey('jobs.Job')
def __str__(self):
return self.job
发票/ models.py
class Invoice(models.Model):
...
class Job(models.Model):
invoice = models.ForeignKey(Invoice)
job = models.ForeignKey('jobs.Job')
def __str__(self):
return self.job
答案 0 :(得分:7)
您需要更改外键中的相关名称,并可能为了清晰起见重命名模型。
# i dont recommended ever naming 2 models the exact same way either
class PurchaseJob(models.Model):
purchase = models.ForeignKey(Purchase, related_name='purchase_job')
class InvoiceJob(models.Model):
invoice = models.ForeignKey(Invoice, related_name='invoice_job')
添加相关名称可以使命名约定保持干净整洁,也可以通过这种方式避免大量的Django错误。