Skip to content
Advertisement

Inherit choice class to extend it?

I have a field in my models.py that accepts choices determined in a class:

JavaScript

The choice class is this:

JavaScript

My doubt is how can I inherit this UserChoices class to another choice class, in order to extend it with another options.

I tried the following:

JavaScript

But it gives me a migration error:

JavaScript

Obviously this example is simplified, the actual code has 40+ choices on the original class and 20+ in the extended one.

Advertisement

Answer

You need to unpack the ones from the parent. You do that with an asterisk (*):

JavaScript

If we unpack a tuple in another tuple, we construct a tuple that contains all the items of the unpacked tuple as elements of the new tuple. For example:

JavaScript

If we thus do not use an asterisk, it will simply see x as a tuple, and we thus construct a 2-tuple with as first element the tuple x.

If we unpack the first tuple, we obtain a 4-tuple where the first three elements originate from x followed by 5.

Advertisement