94👍
Django <= 1.7
This gives you the property names for all related objects:
links = [rel.get_accessor_name() for rel in a._meta.get_all_related_objects()]
You can then use something like this to get all related objects:
for link in links:
objects = getattr(a, link).all()
for object in objects:
# do something with related object instance
I spent a while trying to figure this out so I could implement a kind of “Observer Pattern” on
one of my models. Hope it’s helpful.
Django 1.8+
Use _meta.get_fields()
: https://docs.djangoproject.com/en/1.10/ref/models/meta/#django.db.models.options.Options.get_fields (see reverse in the _get_fields()
source also)
27👍
@digitalPBK was close… here is probably what you are looking for using Django’s built-in stuff that is used in Django admin for displaying related objects during deletion
from django.contrib.admin.utils import NestedObjects
collector = NestedObjects(using="default") #database name
collector.collect([objective]) #list of objects. single one won't do
print(collector.data)
this allows you to create what the Django admin displays – the related objects to be deleted.
- [Django]-Phpmyadmin logs out after 1440 secs
- [Django]-How do you configure Django to send mail through Postfix?
- [Django]-Django filter queryset __in for *every* item in list
9👍
links = [rel.get_accessor_name() for rel in a._meta.get_all_related_objects()]
You can then use something like this to get all related objects:
for link in links:
objects = getattr(a, link.name).all()
for object in objects:
# do something with related object instance
From Django 1.10 offical docs:
MyModel._meta.get_all_related_objects() becomes:
[
f for f in MyModel._meta.get_fields()
if (f.one_to_many or f.one_to_one)
and f.auto_created and not f.concrete
]
So by taking the approved example we would use:
links = [
f for f in MyModel._meta.get_fields()
if (f.one_to_many or f.one_to_one)
and f.auto_created and not f.concrete
]
for link in links:
objects = getattr(a, link.name).all()
for object in objects:
# do something with related object instance
- [Django]-How to create an object for a Django model with a many to many field?
- [Django]-Django-allauth: Linking multiple social accounts to a single user
- [Django]-Django REST Framework – 405 METHOD NOT ALLOWED using SimpleRouter
7👍
Give this a try.
class A(models.Model):
def get_foreign_fields(self):
return [getattr(self, f.name) for f in self._meta.fields if type(f) == models.fields.related.ForeignKey]
- [Django]-How does Django's nested Meta class work?
- [Django]-How do I use CSS in Django?
- [Django]-Django: Arbitrary number of unnamed urls.py parameters
7👍
The following is what django uses to get all related objects
from django.db.models.deletion import Collector
collector = Collector(using="default")
collector.collect([a])
print collector.data
- [Django]-How to filter empty or NULL names in a QuerySet?
- [Django]-Django models avoid duplicates
- [Django]-No handlers could be found for logger
6👍
Django 1.9
get_all_related_objects() has been deprecated
#Example:
user = User.objects.get(id=1)
print(user._meta.get_fields())
Note:
RemovedInDjango110Warning: ‘get_all_related_objects is an unofficial API that has been deprecated. You may be able to replace it with ‘get_fields()’
- [Django]-Django Rest Framework – Updating a foreign key
- [Django]-Django: manage.py does not print stack trace for errors
- [Django]-Can I make an admin field not required in Django without creating a form?
5👍
for link in links:
objects = getattr(a, link).all()
Works for related sets, but not for ForeignKeys. Since RelatedManagers are created dynamically, looking at the class name is easier than doing an isinstance()
objOrMgr = getattr(a, link)
if objOrMgr.__class__.__name__ == 'RelatedManager':
objects = objOrMgr.all()
else:
objects = [ objOrMgr ]
for object in objects:
# Do Stuff
- [Django]-Django REST Framework custom fields validation
- [Django]-Define css class in django Forms
- [Django]-<Django object > is not JSON serializable
2👍
Unfortunately, user._meta.get_fields() returns only relations accessible from user, however, you may have some related object, which uses related_name=’+’. In such case, the relation would not be returned by user._meta.get_fields(). Therefore, if You need generic and robust way to merge objects, I’d suggest to use the Collector mentioned above.
- [Django]-Django Rest Framework custom response message
- [Django]-Where to put business logic in django
- [Django]-How do I do a not equal in Django queryset filtering?
1👍
Here is another way to get a list of fields (names only) in related models.
def get_related_model_fields(model):
fields=[]
related_models = model._meta.get_all_related_objects()
for r in related_models:
fields.extend(r.opts.get_all_field_names())
return fields
- [Django]-How to use Django ImageField, and why use it at all?
- [Django]-How to debug in Django, the good way?
- [Django]-Django Rest Framework custom response message