0

Is it possible to use the CurrentSiteManager to check the site of a related object - not the site of the object itself?

So, given the following models:-

class A(models.Model):
    site = models.ForeignKey(Site)


class B(models.Model):
    a = models.ForeignKey(A)

I want to use the following manager on B:-

    on_site = CurrentSiteManager('a__site')

Otherwise, I'll have to add site as a ForeignKey to both A and B and manage all the issues that come with that.

But it looks like the CurrentSiteManager wants the 'site' field to be on the B class, not on A.

Is my only solution to write my own CurrentSiteManager?

Xantium
  • 11,201
  • 10
  • 62
  • 89
bodger
  • 1,112
  • 6
  • 24

1 Answers1

0

You can use related_name or related query name for this.

DisneylandSC
  • 926
  • 1
  • 5
  • 19
  • Could you explain how? related_name is for the reverse relation from A back to B, no? So I can do a.b_set (or whatever I define related_name to be) but how does that help me here? – bodger May 23 '18 at 17:03
  • Maybe I'm misunderstanding the question, but I think you want to make sure that the records of B that belong to a record in A are included in the same site? But that relation is already created implicitly via the foreignkey. I suggested the related_name since I assumed you were stuck on the reverse relation. – DisneylandSC May 23 '18 at 17:27