Dynamic Foreign Key Reference to different model depending on model choice field

Hello,

i have a little Problem.

I want to reference to different models depending on a choice field in a model.
And has different Foreign Key models.

How did I implement this?

Thanks for your help.

You might be after a GenericForeignKey: https://docs.djangoproject.com/en/3.0/ref/contrib/contenttypes/#generic-relations

Also on Django 3.0 for choices you can use an enumeration type: https://docs.djangoproject.com/en/3.0/releases/3.0/#enumerations-for-model-field-choices . I recently wrote a blog post on moving to them: https://adamj.eu/tech/2020/01/27/moving-to-django-3-field-choices-enumeration-types/

1 Like