2

I have a campaign model as follows:

id campaign    objective             platform
1  Hello Word  MOBILE_APP_ENGAGEMENT Facebook
2  Hi There    VIDEO_VIEWS_PREROLL   Twitter

Model:

class Campaign(Model):
    id = models.TextField(primary_key=True)
    name = models.TextField(default="")
    objective = models.TextField(null=True)
    platform = enumfields.EnumField(Platform, max_length=10, null=True)

The campaign holds both Twitter and FB campaigns.

The objective field was a free text, but I am not happy with it.

I would like to create 2 different enums (enum34):

class FacebookObjective(Enum):
  MOBILE_APP_ENGAGEMENT
  MOBILE_APP_DOWNLOAD

class TwitterObjective(Enum):
  VIDEO_VIEWS_PREROLL
  TWEET_ENGAGEMENTS

and somehow use them on the same column. but not sure how to do it.

I thought to use enum, because I need the other uses to use it easily in the code. e.g:

TwitterObjective.VIDEO_VIEWS_PREROLL
Dejell
  • 13,947
  • 40
  • 146
  • 229

1 Answers1

1

As far as I know (which isn't much where Django is concerned), to make this work you'll need to use a single Enum per field. So in your case I would put the Twitter or FB designation in the name of the members:

Class Objective(Enum):
    FB_MOBILE_APP_ENGAGEMENT
    FB_MOBILE_APP_DOWNLOAD
    TW_VIDEO_VIEWS_PREROLL
    TW_TWEET_ENGAGEMENTS

If you really want to use different Enums you have a couple choices:

  • use nested Enum classes (see https://stackoverflow.com/a/35886825/208880)
  • use two classes and have your implementation choose between them (which will require either embedding a FaceBook or Twitter code in the name, such as FB_ and TW_, or using unique names across the two Enums so you can reverse the lookup when going from db to Python)

This answer may help with the details.

Community
  • 1
  • 1
Ethan Furman
  • 63,992
  • 20
  • 159
  • 237