ValueError save() prohibited to prevent data loss due to unsaved related object '---'.

Raised by:

django/db/models/base.py (line 763)

Traceback (most recent call last):
  File "python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "python3.5/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "python3.5/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "private.py", line 1, in private_function
    private_function()
  File "python3.5/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "private.py", line 1, in private_function
    private_function()
  File "private.py", line 1, in private_function
    private_function()
  File "python3.5/site-packages/django/forms/models.py", line 666, in save
    return self.save_existing_objects(commit) + self.save_new_objects(commit)
  File "python3.5/site-packages/django/forms/models.py", line 800, in save_new_objects
    self.new_objects.append(self.save_new(form, commit=commit))
  File "python3.5/site-packages/django/forms/models.py", line 949, in save_new
    obj.save()
  File "private.py", line 1, in private_function
    private_function()
  File "python3.5/site-packages/django/db/models/base.py", line 763, in save
    "unsaved related object '%s'." % field.name
ValueError: save() prohibited to prevent data loss due to unsaved related object 'event'.

How to reproduce

Given two related models:

class Book(models.Model):
    author = models.ForeignKey('Author')

class Author(models.Model):
    name = models.CharField(max_length=255)

Try saving Book before saving Author:

author = Author(name='Lenine')
book = Book(author=author)
book.save()

How to fix

First save related models before calling save().

Eg.:

class Book(models.Model):
    author = models.ForeignKey('Author')

class Author(models.Model):
    name = models.CharField(max_length=255)

# Right
author = Author(name='Lenine')
authro.save()
book = Book(author=author)
book.save()

# Wrong
author = Author(name='Lenine')
book = Book(author=author)
book.save()