0

Long story short: I have two models, and a database imported from XML. The model layout is as follows:

class A:
    ForgeinKey(B)

class B:
    list = {A1, A2 ... An}

Is there a replacement for {A1, A2 ... An} that would make B.list return a list of A's.

Edit: The idea is to have a field in B that lists all the A's that are pointing to it. I can't seem to figure out how to call A.objects.* from inside B's definition. I don't even know if that's possible.

Edit2: Solved, thanks everyone for help :)

Juho
  • 21
  • 1
  • 3

1 Answers1

0

If I understood correctly, you want B().list to return the result of SELECT * FROM A WHERE B_id = <B.id>?

You have to use related_name, then:

class A(Model):
    b = ForeignKey(B, related_name='list')

Or, you can use the default name, "B().A_set"

redShadow
  • 6,687
  • 2
  • 31
  • 34