Inheritance in Python
-
wrote on 5 Dec 2022, 09:38 last edited by
Hi all!
I'm trying to do some class inheritance in Python
I'm having a little trouble grasping and stuck in this particular section
While working on the particular code mentioned below, I encountered an issue. I have one parent class:class BaseClass(models.Model): email = models.EmailField(blank=True) phone = models.CharField(max_length=32, blank=True) name = models.CharField( max_length=64, blank=True, verbose_name=_(u'name') ) surname = models.CharField( max_length=64, blank=True, verbose_name=_(u'surname') ) class Meta: abstract = True def __str__(self): if self.name: return self.name elif self.email: return self.email else: return self.phone
And I'd like to use all of these data in a child class called Second Class, but I'm not sure what I need to put in the body part of this class:
class SecondClass(BaseClass):
This is the specific code that I have been working on a project after getting few references from the explanations and examples mentioned here, It would be great if anyone could help me.
Thanks in advance! -
Hi all!
I'm trying to do some class inheritance in Python
I'm having a little trouble grasping and stuck in this particular section
While working on the particular code mentioned below, I encountered an issue. I have one parent class:class BaseClass(models.Model): email = models.EmailField(blank=True) phone = models.CharField(max_length=32, blank=True) name = models.CharField( max_length=64, blank=True, verbose_name=_(u'name') ) surname = models.CharField( max_length=64, blank=True, verbose_name=_(u'surname') ) class Meta: abstract = True def __str__(self): if self.name: return self.name elif self.email: return self.email else: return self.phone
And I'd like to use all of these data in a child class called Second Class, but I'm not sure what I need to put in the body part of this class:
class SecondClass(BaseClass):
This is the specific code that I have been working on a project after getting few references from the explanations and examples mentioned here, It would be great if anyone could help me.
Thanks in advance!@Mohit-Singh said in Inheritance in Python:
but I'm not sure what I need to put in the body part of this class
Nothing special.
Just use the inherited members/methods. Don't know what exactly you're asking. -
Hi and welcome to devnet,
Looks like a Django class for use with its ORM.
What exactly is your issue with it ?
1/3