[Answer]-Get all Django cascading dependent models of an auth_user

1👍

You can define a related_name of all ForeignKeys or M2M keys to User model, like this:

# you models.py
class SomeModel(models.Model):
    name = models.CharField(...)
    user = models.ForeignKey(related_name='userrelated__somemodel')

# views.py
# you can get all dependence names of user model attributes
all_user_attrs = dir(request.user)
# ['userrelated__somemodel1', 'userrelated__somemodel2', ...]
# and here you can get all names only related fields
related_fields = filter(lambda x: 'userrelated__' in x, all_user_attrs)
# and if you want to get this field, you can do this:
for attr_name in related_fields:
    attr = getattr(request.user, attr_name, None)
    print attr

Leave a comment