开发者

Django: NameError 'Business' is not defined

Not sure what is causing this error. Please help

NameError at /dash/
name 'Business' is not defined

Exception Location: /home/src/common/models.py in ImageBank, line 38

Here's the model:

class Business(models.Model):
    business_type = models.ManyToManyField(BusinessType)
    business_service_type = models.ManyToManyField(ServiceType)
    establishment_type = models.ForeignKey(EstablishmentType)
    logo = models.ForeignKey(ImageBank)
    phone = PhoneNumberField()
    address = models.ForeignKey(Address)
    website = models.URLField()
    name = models.CharField(max_length=64)

    def __unicode__(self):
        return self.name

The View:

def dashview(request):
    coupon = Coupon.objects.filter()
    bdnspk = request.user.id
    user = request.user.username
    bdns = Business.objects.values('name').get(id=bdnspk)
    开发者_如何学Gocontext = {
        'coupon':coupon,
        'bdns':bdns,
        'user':user


    }
    return render_to_response(
        'dash/dash.html',
        {},
        context,
        context_instance = RequestContext(request),
    )

EDIT: my models is located in /home/src/common/models.py but my django app is in /home/proj/site/ How do I import that?

ImageBank model:

class ImageBank(models.Model):
    business = models.ForeignKey('Business')
    image = models.ImageField(upload_to="images/bank")

    def url(self):
        return self.image.url


Please look at your error: Exception Location: /home/src/common/models.py in ImageBank, line 38 the problem exists in the ImageBank class, which you also seem to be using a ForeignKey reference to in the logo field.

I'm assuming that what the issue is is that you are referencing Business before it is defined as something like a ForeignKey reference inside a field in ImageBank. If this is the case, is ImageBank defined before the Business model inside your models.py? Because doing so will throw this error. The proper way of doing circular ForeignKey references would be to enforce a single ForeignKey with a unique constraint.

Django has this concept built in as a type of field called a OnetoOne field. Have you looked into using a OnetoOne field? See: http://docs.djangoproject.com/en/dev/ref/models/fields/#onetoonefield


Did you import the models in the view? Something like:

from models import Business

at the beginning of the view file


You forgot to import the model in the view, or you're referring to it incorrectly.


If that model is in an app you wrote:

Make sure that the app is listed in INSTALLED_APPS in your settings.py

#settings.py
INSTALLED_APPS = (
    'django....',
    ... more defaults ...,
    'myproject.appname',
)

and at the top of your views

#views.py
from appname.models import Business
#or import all models from that app
from appname.models import *


You are making things a lot more complicated on yourself by having your models.py in a strange unrelated location.

Models can only be imported from python modules so you'll need to make sure that your models.py is in a directory that is a python module and that it is on the python path.

You'll be a whole lot better of just putting your models into an app in your project rather than trying to do something like you are.

More or less you're working against the grain and python is a lot nicer if you work with the grain.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜