24

I've got a Django class like this:

class Breakfast(m.Model):
    # egg = m.OneToOneField(Egg)
    ...

class Egg(m.Model):
    breakfast = m.OneToOneField(Breakfast, related_name="egg")

Is it possible to have breakfast.egg == None if there is no Egg related to the Breakfast?

Edit: Forgot to mention: I'd rather not change the related_name to something like related_name="_egg", then have something like:

@property
def egg(self):
    try:
        return self.egg
    except ...:
        return None

Because I use the name egg in queries, and I'd rather not have to change the queries to using _egg.

6 Answers 6

12

This custom django field will do exactly what you want:

class SingleRelatedObjectDescriptorReturnsNone(SingleRelatedObjectDescriptor):
    def __get__(self, *args, **kwargs):
        try:
            return super(SingleRelatedObjectDescriptorReturnsNone, self).__get__(*args, **kwargs)
        except ObjectDoesNotExist:
            return None

class OneToOneOrNoneField(models.OneToOneField):
    """A OneToOneField that returns None if the related object doesn't exist"""
    related_accessor_class = SingleRelatedObjectDescriptorReturnsNone

To use it:

class Breakfast(models.Model):
    pass
    # other fields

class Egg(m.Model):
    breakfast = OneToOneOrNoneField(Breakfast, related_name="egg")

breakfast = Breakfast()
assert breakfast.egg == None
1
  • 2
    Awesome! BTW in 1.10 interfaces are little changed. SingleRelatedObjectDescriptor is now ReverseOneToOneDescriptor and __get__ parameters are (self, instance, cls=None)
    – Fedor
    Nov 10, 2016 at 22:07
9

I just ran into this problem, and found an odd solution to it: if you select_related(), then the attribute will be None if no related row exists, instead of raising an error.

>>> print Breakfast.objects.get(pk=1).egg
Traceback (most recent call last):
...
DoesNotExist: Egg matching query does not exist

>>> print Breakfast.objects.select_related("egg").get(pk=1).egg
None

I have no idea if this can be considered a stable feature though.

0
5

I know that on ForeignKey you can have null=True when you want to allow the model not to point to any other model. OneToOne is only a special case of a ForeignKey:

class Place(models.Model)
    address = models.CharField(max_length=80)
class Shop(models.Model)
    place = models.OneToOneField(Place, null=True)
    name = models.CharField(max_length=50)
    website = models.URLField()

>>>s1 = Shop.objects.create(name='Shop', website='shop.com')
>>>print s1.place
None
3
  • This isn't quite my problem, though… I want every Egg to belong to a Breakfast, but not every Breakfast necessarily has an Egg. Oct 18, 2010 at 8:16
  • And if you move the OneToOneField to Breakfast with null=True doesn't this give you what you need? I think in that case breakfast.egg can be None, but egg.breakfast will throw DoesNotExists if it can't find it.
    – OmerGertel
    Oct 18, 2010 at 11:21
  • 1
    Not exactly: having the OneToOne field on Breakfast will put the row on the Breakfast table, which is (for various reasons) not what I want. Oct 23, 2010 at 15:06
2

Django 1.10 solution as by Fedor at accepted answer:

from django.core.exceptions import ObjectDoesNotExist
from django.db.models.fields.related import OneToOneField
from django.db.models.fields.related_descriptors import ReverseOneToOneDescriptor

class ReverseOneToOneOrNoneDescriptor(ReverseOneToOneDescriptor):
    def __get__(self, instance, cls=None):
        try:
            return super(ReverseOneToOneOrNoneDescriptor, self).__get__(instance=instance, cls=cls)
        except ObjectDoesNotExist:
            return None

class OneToOneOrNoneField(OneToOneField):
    """A OneToOneField that returns None if the related object doesn't exist"""
    related_accessor_class = ReverseOneToOneOrNoneDescriptor
0
1

OmerGertel did already point out the null option. However, if I understand your logical model right, then what you actually need is a unique and nullable foreign key from Breakfast to Egg. So a breakfast may or may not have an egg, and a particular egg can only be associated with one breakfast.

I used this model:

class Egg(models.Model):
    quality = models.CharField(max_length=50)
    def __unicode__(self):
        return self.quality

class Breakfast(models.Model):
    dish = models.TextField()
    egg = models.ForeignKey(Egg, unique=True, null=True, blank=True)
    def __unicode__(self):
        return self.dish[:30]

and this admin definition:

class EggAdmin(admin.ModelAdmin):
    pass

class BreakfastAdmin(admin.ModelAdmin):
    pass

admin.site.register(Egg, EggAdmin)
admin.site.register(Breakfast, BreakfastAdmin)

Then I could create and assign an egg in the edit page for a breakfast, or just do not assign one. In the latter case, the egg property of the breakfast was None. A particular egg already assigned to some breakfast could not be selected for another one.

EDIT:

As OmerGertel already said in his comment, you could alternatively write this:

    egg = models.OneToOneField(Egg, null=True, blank=True)
1
  • 1
    That would mostly work… But, as with Omer's suggestion: it puts the egg column in the Breakfast table, which (for various reasons) makes life more complex in other ways. Thanks for the answer, though. Oct 23, 2010 at 15:12
0

I would recommend using try / except Egg.DoesNotExist whenever you need to access Breakfast.egg; doing so makes it very clear what's going on for people reading your code, and this is the canonical way of handling nonexistent records in Django.

If you really want to avoid cluttering your code with try / excepts, you could define a get_egg method on Breakfast like so:

def get_egg(self):
    """ Fetches the egg associated with this `Breakfast`.

    Returns `None` if no egg is found.
    """
    try:
        return self.egg
    except Egg.DoesNotExist:
        return None

This will make it clearer to people reading your code that eggs are derived, and it may hint at the fact that a lookup gets performed when one calls Breakfast.get_egg().

Personally, I'd go for the former approach in order to keep things as clear as possible, but I could see why one may be inclined to use the latter approach instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.