mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-02 11:30:12 +03:00
avoid DoesNotExist when setting a non-nullable foreignkey field during post
This commit is contained in:
parent
cc5096e6e6
commit
e9fc1c69b2
|
@ -13,7 +13,7 @@ import warnings
|
|||
from decimal import Decimal, DecimalException
|
||||
from django import forms
|
||||
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.db.models.fields import BLANK_CHOICE_DASH
|
||||
from django.http import QueryDict
|
||||
|
@ -69,9 +69,14 @@ def set_component(obj, attr_name, value):
|
|||
if isinstance(obj, dict):
|
||||
obj[attr_name] = value
|
||||
else:
|
||||
attr = getattr(obj, attr_name, None)
|
||||
if six.callable(attr):
|
||||
raise TypeError("%r.%s is a method; can't set it" % (obj, attr_name))
|
||||
try:
|
||||
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):
|
||||
raise TypeError("%r.%s is a method; can't set it" % (obj, attr_name))
|
||||
setattr(obj, attr_name, value)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user