Sane OneToOne getter

1
2
3
4
5
6
7
8
from django.core.exceptions import ObjectDoesNotExist

def get_onetoone_or_none(obj, attr):
    """ Instead of letting django throw and exception, return None when a OneToOneField is null """
    try:
        return getattr(obj, attr)
    except ObjectDoesNotExist:
        return None

More like this

  1. PartialRequiredMultiValueField by hvdklauw 4 years, 4 months ago
  2. joinstrings filter by Tomek 3 years, 4 months ago
  3. TRAC-Ticket on exception by identify 4 years, 9 months ago
  4. Exclusive boolean field by anentropic 4 years, 4 months ago
  5. Use dateutil's relativedelta to sort an arbitrary list of Django objects by date by cmcavoy 7 years, 1 month ago

Comments

(Forgotten your password?)