Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name is not defined in Django model

Tags:

python

django

I have a Django app with the following in its models.py file:

from django.db import models

class Event(models.Model):
    date = models.DateField()
    name = models.TextField(max_length=60)
    venue = models.ForeignKey(Venue)

    def __unicode__(self):
        return self.name

class Venue(models.Model):
    name = models.TextField(max_length=60)
    street_address = models.TextField(max_length=60)
    locality = models.TextField(max_length=60)
    region = models.TextField(max_length=60)
    postal_code = models.TextField(max_length=60)
    country_name = models.TextField(max_length=60)
    latitude = models.DecimalField(max_digits=9, decimal_places=6)
    longitude = models.DecimalField(max_digits=9, decimal_places=6)

    def __unicode__(self):
        return self.name

But when I run python manage.py syncdb I get the following error:

NameError: name 'Venue' is not defined

Why is this when class Venue is in the file? Have I done something wrong? I’ve just been following the Django tutorial at https://docs.djangoproject.com/en/1.5/intro/tutorial01/.

like image 525
Martin Bean Avatar asked Aug 18 '13 20:08

Martin Bean


1 Answers

Move the definition of Venue before the definition of Event. The reason is that Event references the Venue class in its ForeignKey relationship before Venue is defined.

Or you can do this:

venue = models.ForeignKey('Venue')
like image 88
Uku Loskit Avatar answered Nov 16 '22 08:11

Uku Loskit