Novice Django question about object relationship
I am having some troubles understanding the object relationships in Django. I wrote this:
from django.db import models
class Community(models.Model):
name = models.CharField(max_length=200) #arbitrary max length
class Category(models.Model):
community = models.ForeignKey(Community)
category = models.CharField(max_length=200) #arbitrary max length
class Detail_View(models.Model):
category = models.ForeignKey(Category)
detailView = models.CharField(max_length=200) #arbitrary max length
website = models.CharField(max_length=200) #arbitrary max length
Each community should be able to have several Categories that are associated with it and each category should be able to have several Detail_Views associated with it. I can do what I want with Communities, but as soon as I try to add a Category or Detail View, it screws up. For example, if I say:
c = Community.objects.get(id=1开发者_Go百科)
c.category_set.create(category = "FooBar")
I get an error that says, "DatabaseError: table Content_Management_System_category has no column named category"
You probably haven't run python manage.py syncdb
recently, or you did but you changed some table/models.Model definitions.
Try dropping your database, creating it, and running syncdb again. That is, if you don't have any important data in there.
You can also do python manage.py reset <appname>
This sounds like you forgot to run syncdb after adding Category and/or Detail_View to your models.py
Or that because you have a field and the table name is Category, the models is getting messed up.
Try changing your category field to name in the class Category. Same with detail_view
精彩评论