Reverse accessory

I am trying to assign multiple tasks to an invoice / purchase, however I ran into a problem. Am I missing something? Should it be many-to-many or something else?

I get the following error:

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'.

Purchase / 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

accounts / 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
+4
source share
1 answer

You need to change the associated names in an external key and possibly rename the models for clarity.

# 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')

Adding related names ensures that the naming conventions are clean and tidy, and you avoid a lot of Django errors by doing this.

+6
source

All Articles