Django: list all reverse relations of a model

前端 未结 3 1272
故里飘歌
故里飘歌 2020-12-15 07:59

I would like my django application to serve a list of any model\'s fields (this will help the GUI build itself).

Imagine the classes (ignore the fact that all field

3条回答
  •  佛祖请我去吃肉
    2020-12-15 08:17

    And what about this :

    oneToOneFieldNames = [
        field_name 
        for field_name in Item._meta.get_all_field_names() 
        if isinstance(
            getattr(
                Item._meta.get_field_by_name(field_name)[0], 
                'field', 
                None
            ), 
            models.OneToOneField
        )
    ]
    

    RelatedObject may have a Field attribute for relations. You just have to check if this is a OneToOne field and you can retrieve only what you want

提交回复
热议问题