How to join two models in django-rest-framework

后端 未结 2 764
难免孤独
难免孤独 2020-12-31 08:11

Let say i have two models:

level:

id
file_number
status


level_process:

process_ptr_id
level_id

I want to combine both of my table above

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-31 08:49

    Don't try to "join" tables. This isn't SQL.

    I am assuming your model look like Following,

    class Level(models.Model):
        .......
    
    class LevelProcess(models.Model):
        level = models.ForeignKey(Level)
    

    Now, let's walk for query,

    l = Level.objects.filter(id=level_id).first()
    lp = l.level_process_set.all()
    

    This is how we do in Django ORM.

提交回复
热议问题