I am forgetting OOP terminology which was related to inheritance and which used classes dynamically. Here is what I am looking for.
@classmethod
def _match_slug(cls, slug):
""" Method that checks if we still have a match in the db for current 'slug' """
return cls.objects.filter(slug=slug).count()
I have 10 models in my application and each has this _match_slug
method. I want to take this method to the parent class, so when I call self._match_slug(slug_to_check)
it calls the method with its appropriate class cls
.
Thoughts?
Move the method to your parent class:
class Parent(object):
@classmethod
def _match_slug(cls, slug):
""" Method that checks if we still have a match in the db for current 'slug' """
return cls.objects.filter(slug=slug).count()
class Child(Parent):
...
c = Child()
# Equivalent to Parent._match_slug(c.__class__, x)
c._match_slug(x)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.