Django: list all reverse relations of a model

徘徊边缘 提交于 2019-11-27 16:05:50

问题


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 of Steps could be in Item, I have my reasons :-) )

class Item(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()

class Steps(models.Model):
    item = models.OneToOneField('Item', related_name='steps')
    design = models.BooleanField(default=False)
    prototype = models.BooleanField(default=False)
    production = models.BooleanField(default=False)

Now, when I want to list a model's fields:

def get_fields(model):
    return model._meta.fields + model._meta.many_to_many

But I would also like to get the list of "related" one-to-one foreign keys to my models. In my case Item.steps would not be in that list.

I have found that model._meta.get_all_field_names does include all the related fields.

But when I call Item._meta.get_field_by_name('steps') it returns a tuple holding a RelatedObject, which does not tell me instantly whether this is a single relation or a one-to-many (I want to list only reversed one-to-one relations).

Also, I can use this bit of code:

from django.db.models.fields.related import SingleRelatedObjectDescriptor
reversed_f_keys = [attr for attr in Item.__dict__.values() \
                  if isinstance(attr, SingleRelatedObjectDescriptor)]

But I'm not very satisfied with this.

Any help, idea, tips are welcome!

Cheers


回答1:


This was changed (in 1.8 I think) and Olivier's answer doesn't work anymore. According to the docs, the new way is

[f for f in User._meta.get_fields()
    if f.auto_created and not f.concrete]

This includes one-to-one, many-to-one, and many-to-many.




回答2:


I've found out that there are methods of Model._meta that can give me what I want.

my_model = get_model('app_name','model_name')
# Reverse foreign key relations
reverse_fks = my_model._meta.get_all_related_objects()
# Reverse M2M relations
reverse_m2ms = my_model._meta.get_all_related_many_to_many_objects()

By parsing the content of the relations, I can guess whether the "direct" field was a OneToOneField or whatever.




回答3:


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



来源:https://stackoverflow.com/questions/19512187/django-list-all-reverse-relations-of-a-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!