Last active
May 17, 2018 10:51
-
-
Save tykling/72d3396601c010842695910fbdc74661 to your computer and use it in GitHub Desktop.
Django model method to print information on model instances with an FK relation to the current model instance.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def print_related_objects(self): | |
"""Print info on the objects that are related with a ForeignKey to this instance""" | |
for link in [f for f in self._meta.get_fields() if f.one_to_many and f.auto_created and not f.concrete]: | |
related = getattr(self, link.get_accessor_name()).all() | |
print("found %s instances of %s pointing at this object - they can be accessed using obj.%s.all()" % (len(related), link.related_model, link.get_accessor_name())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In [1]: Delivery.objects.first().print_related_objects()
found 0 instances of <class 'mrxcore.models.SubscriptionTermination'> pointing at this object - they can be accessed using obj.subscriptionterminations.all()
found 2 instances of <class 'radius.models.Radcheck'> pointing at this object - they can be accessed using obj.radchecks.all()
found 2 instances of <class 'radius.models.Radreply'> pointing at this object - they can be accessed using obj.radreplies.all()
found 1 instances of <class 'internetservice.models.InternetService'> pointing at this object - they can be accessed using obj.internetservice_set.all()
found 0 instances of <class 'tvservice.models.VODREntry'> pointing at this object - they can be accessed using obj.vodrentries.all()
found 0 instances of <class 'tvservice.models.TvService'> pointing at this object - they can be accessed using obj.tvservice_set.all()
found 1 instances of <class 'phoneservice.models.PhoneService'> pointing at this object - they can be accessed using obj.phoneservice_set.all()
found 0 instances of <class 'customer.models.ContactInfoOverride'> pointing at this object - they can be accessed using obj.contactinfooverride.all()
found 0 instances of <class 'customer.models.Order'> pointing at this object - they can be accessed using obj.orders.all()
found 16 instances of <class 'partners.models.WaooSoapRequest'> pointing at this object - they can be accessed using obj.waoosoaprequests.all()
found 1 instances of <class 'workers.models.PortProvisioningJob'> pointing at this object - they can be accessed using obj.portprovisioningjobs.all()
found 5 instances of <class 'notes.models.DeliveryNote'> pointing at this object - they can be accessed using obj.notes.all()
found 1 instances of <class 'booking.models.TechBooking'> pointing at this object - they can be accessed using obj.techbookings.all()
found 2 instances of <class 'panther.models.PantherRequest'> pointing at this object - they can be accessed using obj.pantherrequests.all()
found 1 instances of <class 'wifiservice.models.WifiService'> pointing at this object - they can be accessed using obj.wifiservice_set.all()
found 0 instances of <class 'waoostreamingservice.models.WaooStreamingService'> pointing at this object - they can be accessed using obj.waoostreamingservice_set.all()