akash deep
akash deep

Reputation: 165

Validate soft foreign key Django

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

Answers (2)

Waldemar Podsiadło
Waldemar Podsiadło

Reputation: 1412

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

Upvotes: 0

0sVoid
0sVoid

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

Related Questions