S. Ellis
S. Ellis

Reputation: 113

How to create an instance of an object with inherited from a parent fields?

I have two classes: Word and its child Verb:

class Word(models.Model):
    original = models.CharField(max_length=40)
    translation = models.CharField(max_length=40)

class Verb(Word):
    group = models.IntegerField(default=1)
    past_form= models.CharField(max_length=40)
    future_form = models.CharField(max_length=40)

First, I created an instance of Word:

new_word = Word()

I did some processing with this instance, and now I need to create an instance of Verb, based on new_word.

I tried to do it like this:

new_verb = Verb(new_word)

but looks like this is not the right way.

Important remark: I cannot override __init__ method, because Word inherits from django model

Upvotes: 0

Views: 46

Answers (1)

Stepan
Stepan

Reputation: 1054

In your example you use class Verb(Word) construction which is used for inheritance of Word class properties and methods to Verb class.

To use properties of Word instance when creating Verb you need to add constructor (init method) into your Verb class like this:

class Verb(Word):
    def __init__(self, Word):
        # use Word properties to initialize Verb class

Upvotes: 1

Related Questions