2👍
✅
Use .first()
.
data['Sponsor'] = models.Family.objects.filter(Dependent=data['member']).first()
If there are no items, it will return None
.
👤AKX
-1👍
It throws a DoesNotExist
exception if that record does not exist.
You can catch that and handle as needed.
try:
data['Sponsor'] = models.Family.objects.get(Dependent=data['member'])
except Family.DoesNotExist:
data['Sponsor'] = None
- Change the css class of an element in a Django form that uses ModelForm
- Django dynamics ChoiceFields choices from APIs raises ValueError
- Is it possible to add a new function to the class based view of django rest framework
- Django 3rd party packages not working
- Set slug field form manually in views Django
Source:stackexchange.com