In python django how do you print out an object's inrospection? The list of all public methods of that object (variable and/or functions)?
e.g.:
def Factotum(models.Model): id_ref = models.IntegerField() def calculateSeniorityFactor(): return (1000 - id_ref) * 1000
I want to be able to run a command line in the Django shell to tell me all of the public methods of a Django model. The output of running on above would be:
>> introspect Factotoum --> Variable: id_ref --> Methods: calculateSeniorityFactor
Python – all() function The Python all() function returns true if all the elements of a given iterable (List, Dictionary, Tuple, set, etc.)
You can use the built in dir() function to get a list of all the attributes a module has. Try this at the command line to see how it works. Also, you can use the hasattr(module_name, "attr_name") function to find out if a module has a specific attribute.
str function in a django model returns a string that is exactly rendered as the display name of instances for that model. # Create your models here. This will display the objects as something always in the admin interface.
Well, things you can introspect are many, not just one.
Good things to start with are:
>>> help(object)
>>> dir(object)
>>> object.__dict__
Also take a look at the inspect module in the standard library.
That should make 99% of all the bases belong to you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With