Is there a way to create a django model foreignkey field with variable choices?

Let's say I have three models device, mechanical and digital. In the device model I have a field type. The type field needs to be in a foreign key relationship with either mechanical or digital model which will be determined by the data. Is there any way to create the type field in such a way that the model in which the foreign key relationship is to be done can be chosen manually. Something like: type = models.ForeignKey(to=Choices) where the Choices could be digital and mechanical.

I have tried implementing generic foreign keys but my database schema is a bit complex so that would be very difficult to maintain. Is there any other way in django that I can do the above?


A generic way that you can manage this would be to use your own models.

Create a ChoiceItemGroup model, slug, name, description.

Create another model ChoiceItem, with slug, name, description, and FK to ChoiceItemGroup.

Then you can do:

type=models.ForeignKey(ChoiceItem, on_delete=models.CASCADE)

Then you can create generic choices and generic choice groups by registering the models in the admin panel