# Buggy code
def save(self, no_signals=False, commit=True, **kwargs):
    """
    Args:
        commit: True if model should be really saved
    """
    # delete template cache
    if hasattr(self, "_template_cache"):
        delattr(self, "_template_cache")

    created = not bool(self.pk)
    if self.reverse_id == "":
        self.reverse_id = None
    if self.application_namespace == "":
        self.application_namespace = None
    from cms.utils.permissions import _thread_locals

    user = getattr(_thread_locals, "user", None)
    if user:
        try:
            changed_by = force_text(user)
        except AttributeError:
            # AnonymousUser may not have USERNAME_FIELD
            changed_by = "anonymous"
        else:
            # limit changed_by and created_by to avoid problems with Custom User Model
            if len(changed_by) > constants.PAGE_USERNAME_MAX_LENGTH:
                changed_by = "{0}... (id={1})".format(
                    changed_by[: constants.PAGE_USERNAME_MAX_LENGTH - 15],
                    user.pk,
                )

        self.changed_by = changed_by

    else:
        self.changed_by = "script"
    if created:
        self.created_by = self.changed_by

    if commit:
        if not self.depth:
            if self.parent_id:
                self.parent.add_child(instance=self)
            else:
                self.add_root(instance=self)
            return  # add_root and add_child save as well
        super(Page, self).save(**kwargs)


# Traceback (AttributeError)
Traceback (most recent call last):
File "/home/user/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/user/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/utils/decorators.py", line 105, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/user/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/views/decorators/cache.py", line 52, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/user/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 206, in inner
return view(request, *args, **kwargs)
File "/home/user/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/db/transaction.py", line 394, in inner
return func(*args, **kwargs)
File "/home/user/.virtualenvs/myenv/local/lib/python2.7/site-packages/cms/admin/pageadmin.py", line 848, in move_page
page.move_page(target, position)
File "/home/user/.virtualenvs/myenv/local/lib/python2.7/site-packages/cms/models/pagemodel.py", line 216, in move_page
moved_page = self.move(target, pos=position)
File "/home/user/.virtualenvs/myenv/local/lib/python2.7/site-packages/cms/models/pagemodel.py", line 1223, in move
super(Page, self).move(target, pos)
File "/home/user/.virtualenvs/myenv/local/lib/python2.7/site-packages/treebeard/mp_tree.py", line 1037, in move
return MP_MoveHandler(self, target, pos).process()
File "/home/user/.virtualenvs/myenv/local/lib/python2.7/site-packages/treebeard/mp_tree.py", line 444, in process
if self.target.is_descendant_of(self.node):
AttributeError: 'NoneType' object has no attribute 'is_descendant_of'

# Fixed code
def save(self, no_signals=False, commit=True, **kwargs):
    """
    Args:
        commit: True if model should be really saved
    """
    # delete template cache
    if hasattr(self, "_template_cache"):
        delattr(self, "_template_cache")

    created = not bool(self.pk)
    if self.reverse_id == "":
        self.reverse_id = None
    if self.application_namespace == "":
        self.application_namespace = None
    from cms.utils.permissions import _thread_locals

    user = getattr(_thread_locals, "user", None)
    if user:
        try:
            changed_by = force_text(user)
        except AttributeError:
            # AnonymousUser may not have USERNAME_FIELD
            changed_by = "anonymous"
        else:
            # limit changed_by and created_by to avoid problems with Custom User Model
            if len(changed_by) > constants.PAGE_USERNAME_MAX_LENGTH:
                changed_by = "{0}... (id={1})".format(
                    changed_by[: constants.PAGE_USERNAME_MAX_LENGTH - 15],
                    user.pk,
                )

        self.changed_by = changed_by

    else:
        self.changed_by = "script"
    if created:
        self.created_by = self.changed_by

    if commit:
        if not self.depth:
            if self.parent_id:
                self.depth = self.parent.depth + 1
                self.parent.add_child(instance=self)
            else:
                self.add_root(instance=self)
            return  # add_root and add_child save as well
        super(Page, self).save(**kwargs)