43๐
How are you calling this method? You have defined an instance method, which can only be called on an instance of the class, not the class itself. In other words, once you have an instance of model
called mymodelinstance
, you can do mymodelinstance.my_custom_method()
.
If you want to call it on the class, you need to define a classmethod. You can do this with the @classmethod
decorator on your method. Note that by convention the first parameter to a classmethod is cls
, not self
. See the Python documentation for details on classmethod.
However, if what you actually want to do is to add a method that does a queryset-level operation, like objects.filter()
or objects.get()
, then your best bet is to define a custom Manager and add your method there. Then you will be able to do model.objects.my_custom_method()
. Again, see the Django documentation on Managers.
0๐
If you are implementing a Signal for your model it does not necessarily need to be defined in the model class only. You can define it outside the class and pass the class name as the parameter to the connect function.
However, in your case you need the model object to access the method.
- Django: how to change values for nullbooleanfield in a modelform?
- Django south: changing field type in data migration
- Django 1.7 upgrade error: AppRegistryNotReady: Models aren't loaded yet