Martin Bean
Martin Bean

Reputation: 39439

Name is not defined in Django model

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/.

Upvotes: 13

Views: 20212

Answers (1)

Uku Loskit
Uku Loskit

Reputation: 42050

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')

Upvotes: 30

Related Questions