[Answer]-NameError: name 'Tag' is not defined

1👍

You declare Tag after you declare Picture, but Picture uses Tag, so it’s not defined at the time you try to use it. Just change the order of your classes and it should solve things.

In other words, change your code to:

class Tag(models.Model):
    pics = models.ManyToManyField(Picture)
    name = models.CharField(max_length=30)

# Hurray, Tag exists now

class Picture(models.Model):
    name = models.CharField(max_length=100)
    pub_date = models.DateTimeField('date published')

    # Therefore, this next line will work

    tags = models.ManyToManyField(Tag)
    owner = models.ForeignKey(User)

(minus my comments)

Leave a comment