How to query directly the table created by Django for a ManyToMany relation?

前端 未结 1 515
终归单人心
终归单人心 2021-02-01 14:21

I have a model MyModel2 with a ManyToManyField related to another model MyModel1.

How can I get the pairs mymodel1.id, mymode

相关标签:
1条回答
  • 2021-02-01 14:46

    This is the many to many field instance:

    MyModel2.mymodel1
    

    This is the intermediary table model:

    MyModel2.mymodel1.through
    

    This is the intermediary model manager:

    MyModel2.mymodel1.through.objects
    

    This returns a queryset for all intermediary models:

    MyModel2.mymodel1.through.objects.all()
    

    This part of django docs talk about through. You can make a through model yourself, else it is automatically generated.

    0 讨论(0)
提交回复
热议问题