0

I have moved NEW_MESSAGE_ON_PROJECT and NEW_MESSAGE_ON_PROPOSAL to BaseNotification, the superclass.

class CustomerNotification(BaseNotification):
    NEW_PROPOSAL = 1000
    # NEW_MESSAGE_ON_PROJECT = 1001
    # NEW_MESSAGE_ON_PROPOSAL = 1002
    CHOICES = ((NEW_PROPOSAL, "New Prpposal"),
               (NEW_MESSAGE_ON_PROJECT, "New Message on Project"),
               (NEW_MESSAGE_ON_PROPOSAL,"New Message on Proposal"))

When I set CHOICES, I get

NameError: name 'NEW_MESSAGE_ON_PROJECT' is not defined

I dont get self context here. So whats the solution?

Jesvin Jose
  • 22,498
  • 32
  • 109
  • 202

2 Answers2

2

You have to explicitly use the name of the base class:

class CustomerNotification(BaseNotification):
    NEW_PROPOSAL = 1000
    # NEW_MESSAGE_ON_PROJECT = 1001
    # NEW_MESSAGE_ON_PROPOSAL = 1002
    CHOICES = ((NEW_PROPOSAL, "New Prpposal"),
               (BaseNotification.NEW_MESSAGE_ON_PROJECT, 
                "New Message on Project"),
               (BaseNotification.NEW_MESSAGE_ON_PROPOSAL,
                "New Message on Proposal"))

The reason for this is that you get a clean namespace when starting a class definition, similar (but certainly not equal) to the namespace you get when you start a function.

This namespace does not include anything from the base class. Accessing base class members is handled when accessing members of the finished class (or their objects, for that matter).

Jonas Schäfer
  • 20,140
  • 5
  • 55
  • 69
  • Could you elaborate on last paragraph? Are base class members available only when you instantiate an object? – Jesvin Jose Feb 12 '15 at 09:27
  • No, you can also access base class members using the class – but only after the class declaration is finished. Like, after the declaration ``CustomerNotification.NEW_MESSAGE_ON_PROJECT`` works as expected. – Jonas Schäfer Feb 12 '15 at 09:33
0

You'll need to reference BaseNotification.NEW_PROPOSAL directly.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895