34π
This was introduced in Django 1.8. Previously you could assign not saved instance to One-To-One relation and in case of fail it was silently skipped. Starting from Django 1.8 you will get error message in this case.
Check a documentation of Django 1.7 -> 1.8 upgrade.
It says:
Assigning unsaved objects to a ForeignKey, GenericForeignKey, and
OneToOneField now raises a ValueError.
If you are interested in more details, you can check save
method in django.db.models.base
: Some part of it:
for field in self._meta.concrete_fields:
if field.is_relation:
# If the related field isn't cached, then an instance hasn't
# been assigned and there's no need to worry about this check.
try:
getattr(self, field.get_cache_name())
except AttributeError:
continue
obj = getattr(self, field.name, None)
# A pk may have been assigned manually to a model instance not
# saved to the database (or auto-generated in a case like
# UUIDField), but we allow the save to proceed and rely on the
# database to raise an IntegrityError if applicable. If
# constraints aren't supported by the database, there's the
# unavoidable risk of data corruption.
if obj and obj.pk is None:
raise ValueError(
"save() prohibited to prevent data loss due to "
"unsaved related object '%s'." % field.name
)
Last 5 rows are where this error is raised. basically your related obj
which is not saved will have obj.pk == None
and ValueError
will be raised.
42π
it is simple:
p3 = Place(name='Demon Dogs', address='944 W. Fullerton')
p3.save() # <--- you need to save the instance first, and then assign
Restaurant.objects.create(
place=p3, serves_hot_dogs=True, serves_pizza=False
)
- [Django]-How can I activate the unaccent extension on an already existing model
- [Django]-Django: Group by date (day, month, year)
- [Django]-Django, creating a custom 500/404 error page
4π
Answered β The problem arose from django not saving empty or unchanged forms. This led to null fields on those unsaved forms. Problem was fixed by allowing null fields on foreign keys, as a matter of fact β all fields. That way, empty or unchanged forms did not return any errors on save.
FYI: Refer to @wolendranh answer.
- [Django]-How do I use django rest framework to send a file in response?
- [Django]-Linking to the django admin site
- [Django]-Create if doesn't exist
0π
I just removed my model.save()
and the error went away.
This only works if you are saving it when there are no changes.
Otherwise you should save it one time, if you changed something in the queryset.
an example:
views.py
queryset = myModel.objects.get(name="someName", value=4)
queryset.value = 5
# here you need to save it, if you want to keep the changes.
queryset.save()
...
# If you save it again, without any changes, for me I got the error save() prohibited to prevent data loss due to unsaved related object
# don't save it again, unless you have changes.
# queryset.save()
- [Django]-How to use permission_required decorators on django class-based views
- [Django]-Can you change a field label in the Django Admin application?
- [Django]-How to use python2.7 pip instead of default pip
0π
save
will work in many cases, but there are cases when youβd not like to save one object if later on due to some error another object saving fails.
Basically you want to rollback the changes. If you donβt handle this case properly you might end up having corrupt data.
Django already provides atomic transaction api to handle such cases. Encapsulate all of the model changes in one function and wrap it with the atomic transaction.
- [Django]-How to avoid AppConfig.ready() method running twice in Django
- [Django]-How to query Case-insensitive data in Django ORM?
- [Django]-Django β Website Home Page