Search code examples
pythondjangofunctional-testing

DJANGO can't delete a user from django.contrib.auth.models when running functional test


I have not been able to execute the deletion of any User that is created during test while tearDown() methods are run. Error is puzzling, as it suggest no models are loaded.

have tried the following:

User.objects.filter(id__in=users_to_remove).delete()
User.objects.filter(username='testuser2').delete()

And both return the same error message.

Does anyone know what is the propper way to delete a user from the testing file?

Detail of Functional Test File: https://gist.github.com/leoreq/af090569980f06985f83

Error Message Returned: Erorr Message returned in terminal


Solution

  • You need to call django.setup() after setting the environment variable.

    # Set up django
    import os
    import sys
    import django
    project_dir = abspath(dirname(dirname(__file__)))
    sys.path.insert(0, project_dir)
    os.environ['DJANGO_SETTINGS_MODULE'] = 'slapp.settings'
    django.setup()