Home > database >  Validate soft foreign key Django
Validate soft foreign key Django

Time:07-15

Have a model in django that has a json field with list of IDs to a different model. How can we validate the inputs to that field are valid foreign key.

Not using many to many field or a joining model separately.

CodePudding user response:

This seems like a strange way of doing things but your best option would be to get a list of your valid IDs with MyModel.objects.all().values_list('id', flat=True) and compare your JSON data with the resulting list

CodePudding user response:

Another way to do it is compere jsonlist lenght with this count: MyModel.objects.filter(id__in=jsonlist).count()

  • Related