Thiago Maltempi
Thiago Maltempi

Reputation: 78

Django Two Many to one fields in entity

Following django doc for many-to-one, they has the situation:

  class Reporter(models.Model):
    ...

  class Article(models.Model):
    reporter = models.ForeignKey(Reporter)
    ...

If I need get all Articles from Reporter, I could do: reporter.article_set.all(). So far, so good.

But, imagine that the Article need have a Auxiliary Reporter (reporter_aux).

class Article(models.Model):
    reporter = models.ForeignKey(Reporter, related_name='reporter')
    reporter_aux = models.ForeignKey(Reporter, related_name='reporter_aux')
    ...

How I could get all the articles that a reporter was auxiliary (from reporter_aux)?

Thanks!!!

Upvotes: 1

Views: 100

Answers (1)

Rami Olaqi
Rami Olaqi

Reputation: 56

you will need to edit your related name to somthing else; for example

article_reporter_aux

you can do a Django reverse lookup for the related_name

reporter.article_reporter_aux.all()

Upvotes: 1

Related Questions