avoid DoesNotExist when setting a non-nullable foreignkey field during post

This commit is contained in:
Craig de Stigter 2013-09-03 12:36:54 +12:00
parent cc5096e6e6
commit e9fc1c69b2

View File

@ -13,7 +13,7 @@ import warnings
from decimal import Decimal, DecimalException from decimal import Decimal, DecimalException
from django import forms from django import forms
from django.core import validators from django.core import validators
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError, ObjectDoesNotExist
from django.conf import settings from django.conf import settings
from django.db.models.fields import BLANK_CHOICE_DASH from django.db.models.fields import BLANK_CHOICE_DASH
from django.http import QueryDict from django.http import QueryDict
@ -69,7 +69,12 @@ def set_component(obj, attr_name, value):
if isinstance(obj, dict): if isinstance(obj, dict):
obj[attr_name] = value obj[attr_name] = value
else: else:
try:
attr = getattr(obj, attr_name, None) attr = getattr(obj, attr_name, None)
except ObjectDoesNotExist:
# happens for non-null FK fields that haven't yet been set.
pass
else:
if six.callable(attr): if six.callable(attr):
raise TypeError("%r.%s is a method; can't set it" % (obj, attr_name)) raise TypeError("%r.%s is a method; can't set it" % (obj, attr_name))
setattr(obj, attr_name, value) setattr(obj, attr_name, value)