Django model - interface design to avoid need of passing object in CHILD class calling method defined in PARENT class
Django model - interface design to avoid need of passing object in CHILD class calling method defined in PARENT class
I am using django 2.0.8 and Python 3.5. I have written a base class which encapsulates behavior in a base class.
When using the interface in the child class, I find that I have to pass the object of the child class to the parent - which is not only ugly, is error prone.
I do not want to use composition (instead of an interface), because AFAIK fields in django models are saved to the DB - that aside, I prefer the sub classing approach, since all the functionality can remain in the base class.
Is there any way I can (in the parent class), find/obtain the instance (or at least the name of the class and it's id) that invoked the method call?
Here is my code:
class Likeable(models.Model):
likes = GenericRelation(Like)
def action_is_permissible(self, actionable_object, actor):
ct = ContentType.objects.get_for_model(actionable_object)
object_id = actionable_object.id
found_objects = Like.objects.filter(content_type=ct, object_id=object_id, liker=actor)
return ((len(found_objects) == 0), ct, object_id, found_objects)
def add_like(self, actionable_object, actor):
can_add, ct, object_id, found_objects = self.action_is_permissible(actionable_object, actor)
if can_add:
like = self.likes.create(content_type=ct, object_id=object_id, liker=actor)
else:
# do nothing
return
class Meta:
abstract = True
class Foo(Likeable):
name = models.CharField(max_length=255,default='')
objects = models.Manager()
foo = Foo.objects.get(id=1)
p = User.objects.get(id=1)
foo.add_like(foo, p) # <- nasty API calling convention
1 Answer
1
You can access it using self
.self
refers to the object which is calling.
self
self
Ref: What is the purpose of self?
EDIT (code changes):
class Likeable(models.Model):
def add_like(self, actor):
# update `actionable_object` to `self`
foo.add_like(p)
foo.add_like(self, p)
self
add_like
You dont have to pass
self
yourself. Python does that for you.– Sachin Kukreja
Sep 5 '18 at 17:28
self
Sigh ... you don't seem to grasp what the problem is here. The method is implemented in the subclass (i.e. parent class of Foo): Likeable. At the point where the method is called,
self
points to an instance of the Likeable
class. The problem is that I don't know how (or indeed if it's possible) to obtain an instance of the CHILD (in this case Foo) who invoked the method at the point that the code is being executed from within the Likeable
class. Put simply, I'm trying to "reach out" from the Likeable
class to see which class invoked the method.– Homunculus Reticulli
Sep 5 '18 at 17:34
self
Likeable
Likeable
Likeable
"
The method is implemented in the subclass
". I think you meant that the method is implemented in the parent class. And, I tried this code. self
does refer to foo
and self.id
prints out the id
of the foo
object.– Sachin Kukreja
Sep 5 '18 at 17:44
The method is implemented in the subclass
self
foo
self.id
id
foo
You're right, I mispoke. I sometimes get confused switching over from C++ and other languages (where the naming convention seems to be different) to Python. method is implemented in the parent class.
– Homunculus Reticulli
Sep 5 '18 at 17:48
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Look at the usage example. I can't invoke
foo.add_like(self, p)
in code - asself
would be unidentified. Look at the signature of theadd_like
method in the base class.– Homunculus Reticulli
Sep 5 '18 at 17:27