mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
Simplified Model.save() a bit.
This commit is contained in:
parent
67b6cb7723
commit
fcbc502af9
1
AUTHORS
1
AUTHORS
@ -305,6 +305,7 @@ answer newbie questions, and generally made Django that much better:
|
|||||||
Gasper Koren
|
Gasper Koren
|
||||||
Gasper Zejn <zejn@kiberpipa.org>
|
Gasper Zejn <zejn@kiberpipa.org>
|
||||||
Gavin Wahl <gavinwahl@gmail.com>
|
Gavin Wahl <gavinwahl@gmail.com>
|
||||||
|
Ge Hanbin <xiaomiba0904@gmail.com>
|
||||||
geber@datacollect.com
|
geber@datacollect.com
|
||||||
Geert Vanderkelen
|
Geert Vanderkelen
|
||||||
George Karpenkov <george@metaworld.ru>
|
George Karpenkov <george@metaworld.ru>
|
||||||
|
@ -678,28 +678,29 @@ class Model(metaclass=ModelBase):
|
|||||||
# been assigned and there's no need to worry about this check.
|
# been assigned and there's no need to worry about this check.
|
||||||
if field.is_relation and field.is_cached(self):
|
if field.is_relation and field.is_cached(self):
|
||||||
obj = getattr(self, field.name, None)
|
obj = getattr(self, field.name, None)
|
||||||
|
if not obj:
|
||||||
|
continue
|
||||||
# A pk may have been assigned manually to a model instance not
|
# A pk may have been assigned manually to a model instance not
|
||||||
# saved to the database (or auto-generated in a case like
|
# saved to the database (or auto-generated in a case like
|
||||||
# UUIDField), but we allow the save to proceed and rely on the
|
# UUIDField), but we allow the save to proceed and rely on the
|
||||||
# database to raise an IntegrityError if applicable. If
|
# database to raise an IntegrityError if applicable. If
|
||||||
# constraints aren't supported by the database, there's the
|
# constraints aren't supported by the database, there's the
|
||||||
# unavoidable risk of data corruption.
|
# unavoidable risk of data corruption.
|
||||||
if obj:
|
if obj.pk is None:
|
||||||
if obj.pk is None:
|
# Remove the object from a related instance cache.
|
||||||
# Remove the object from a related instance cache.
|
if not field.remote_field.multiple:
|
||||||
if not field.remote_field.multiple:
|
field.remote_field.delete_cached_value(obj)
|
||||||
field.remote_field.delete_cached_value(obj)
|
raise ValueError(
|
||||||
raise ValueError(
|
"save() prohibited to prevent data loss due to "
|
||||||
"save() prohibited to prevent data loss due to "
|
"unsaved related object '%s'." % field.name
|
||||||
"unsaved related object '%s'." % field.name
|
)
|
||||||
)
|
elif getattr(self, field.attname) is None:
|
||||||
elif getattr(self, field.attname) is None:
|
# Use pk from related object if it has been saved after
|
||||||
# Use pk from related object if it has been saved after
|
# an assignment.
|
||||||
# an assignment.
|
setattr(self, field.attname, obj.pk)
|
||||||
setattr(self, field.attname, obj.pk)
|
|
||||||
# If the relationship's pk/to_field was changed, clear the
|
# If the relationship's pk/to_field was changed, clear the
|
||||||
# cached relationship.
|
# cached relationship.
|
||||||
if obj and getattr(obj, field.target_field.attname) != getattr(self, field.attname):
|
if getattr(obj, field.target_field.attname) != getattr(self, field.attname):
|
||||||
field.delete_cached_value(self)
|
field.delete_cached_value(self)
|
||||||
|
|
||||||
using = using or router.db_for_write(self.__class__, instance=self)
|
using = using or router.db_for_write(self.__class__, instance=self)
|
||||||
|
Loading…
Reference in New Issue
Block a user