I am working on a Python/Django/Wagtail project, and at some point I have a class like this:
class SuperClass(BaseClass):
body = StreamField([
('overview_speakers', OverviewSpeakers()),
])
def some_function():
return 'Hola'
OverviewSpeakers is a class that is expecting an argument and I want to try to pass in the results of some_function()
I tried both:
body = StreamField([
('overview_speakers', OverviewSpeakers(self.some_function())),
])
and
body = StreamField([
('overview_speakers', OverviewSpeakers(SuperClass.some_function())),
])
But respectively it yells that self or SuperClass are not defined.
What can I do to pass in the results of the function?
Check this out:
class SuperClass(BaseClass):
def some_function():
return 'Hola'
body = StreamField([
('overview_speakers', OverviewSpeakers(some_function())),
])
You cannot refer to a class that is being defined, but you can add attibutes to it after it is defined. So this should work:
class SuperClass(BaseClass):
def some_function():
return 'Hola'
SuperClass.body = StreamField([
('overview_speakers', OverviewSpeakers(SuperClass.some_function())),
])