Reputation: 165
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.
Upvotes: 0
Views: 118
Reputation: 1412
Another way to do it is compere jsonlist lenght with this count: MyModel.objects.filter(id__in=jsonlist).count()
Upvotes: 0
Reputation: 2663
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
Upvotes: 1