[Answered ]-How to deal with empty foreign keys in a for loop within django View

1👍

You can catch models.DoesNotExist exception.

def someview(request):
    shift_users = TeamMember.objects.all()
    weekday_shifts = WeekdayAssignment.objects.all()
    for t in shift_users:
        for ws in weekday_shifts:
            try:
                print(ws.triage_nam_3)
            except TeamMember.DoesNotExist:
                continue

Leave a comment