Home > Software design >  How to apply multiple field options to one field in Django
How to apply multiple field options to one field in Django

Time:12-31

I'm using Django, is there a way to apply foreign key and ChartField to one field at the same time? Sometimes I want to allow the user to enter a value that is not in the foreign key. I've googled for a long time and found various ways, but I can't find a solution. Please help.

[models.py]

class Supporting(models.Model):
    assignment = models.ForeignKey(Assignment, on_delete=models.CASCADE, blank=False, null=True)

CodePudding user response:

Foreign key only allows ids or objects of models that it has relation with. it can never ever accept text. if you want to this field to be a foreign key for other models too then there is a solution. you can use generic foreign key

CodePudding user response:

You Might be looking for a through Model between Supporting and Assignment. As per your question, you might require multiple fields defining the nature of relation between two models and Through Models are exactly made for that!

Django Through Models Docs

  • Related