[Django]-Fresh mysql database getting "mydb.background_task doesn't exist" – Django

3👍

Looks like there is a issue in the library see https://github.com/arteria/django-background-tasks/issues/204. You may want to temporarily delete your code to migrate your database.

3👍

Try doing the following from the Documentation:

Install from PyPI:

pip install django-background-tasks

Add to INSTALLED_APPS inside your settings.py.

INSTALLED_APPS = (
# ...
'background_task',
# ...
)

Check that everything is fine with your migrations.

python manage.py showmigrations

Migrate your database and check if the background_task file is added to the migrations list.

python manage.py migrate

Make modifications in your views in which ever way you want to use the background_tasks. For example, you can look up a userID and send them message.

from background_task import background
from django.contrib.auth.models import User

@background(schedule=60)
def notify_user(user_id):
    # lookup user by id and send them a message
    user = User.objects.get(pk=user_id)
    user.email_user('Here is a notification', 'You have been notified')

3👍

It sounds like you didn’t delete the contents of each pycache folder, which means Django will still try to reference non-existent migrations.

Here is an easy-to-use shortcut for clearing migrations, which I found from this blog post. Just run the following from the CLI in your root project folder:

find . -path "*/migrations/*.py" -not -name "__init__.py" -not -path "./env/*" -delete
find . -path "*/migrations/*.pyc" -not -path "./env/*" -delete

Afterwards, delete your sqlite db, and try the migrations again.

If you run this in the wrong directory, you will break your Django download, along with any 3rd party Django apps that use migrations!

I use this frequently, so I went ahead and made it a bash function:

function djclear() {
  dir="${PWD##*/}"
  if [ -d $dir ]; then
       find . -path "*/migrations/*.py" -not -name "__init__.py" -not -path "./env/*" -delete
       find . -path "*/migrations/*.pyc" -not -path "./env/*" -delete
  else
    echo "Returning with exit code 1. Are you sure that you are in a project directory?"
    return 1
  fi
}

This looks complicated, but all it really does is confirm that you are in a Django Project folder before executing the command.

Leave a comment