Django get_object_or_404 is not defined

时间:2016-08-31 18:23:16

标签: python django import

I am developing a standalone application which uses ORM of django. In my main application, I am using django's module of get_object_or_404.

I have imported it with all its dependencies when I run the script, it gives me the error:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 240, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 438, in __protected_call__
    return self.run(*args, **kwargs)
  File "/root/standAlone/tasks.py", line 48, in task1
NameError: global name 'get_object_or_404' is not defined

Here is my full script code:

import django
from celery import Celery
from django.conf import settings
settings.configure(
    DATABASE_ENGINE    = "django.db.backends.mysql",
    DATABASE_NAME      = "database name",
    DATABASE_USER      = "username",
    DATABASE_PASSWORD  = "password",
    DATABASE_HOST      = "host",
    DATABASE_PORT      = "3306",
    INSTALLED_APPS     = ("myApp",)
)
django.setup()
from django.db import models
from myApp.models import *
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render,redirect
from django.shortcuts import get_list_or_404, get_object_or_404
from celery.decorators import task
from celery.utils.log import get_task_logger



app = Celery('tasks', broker='redis://broker_url')

@app.task(name="task1")
def task1(recipe_pk):
    recipe = get_object_or_404(Recipe, pk=recipe_pk) #error occurs here
    recipe.status = 'Completed'
    recipe.save()

Does anyone know how to solve this problem?

1 个答案:

答案 0 :(得分:0)

Try to remove this double import of "django.shortcuts".