我对Django有点新鲜,我似乎无法找到解决方案。我好几天都在打沙子。
我有一个SQLite3数据库,在通过我的视图处理它时,我的所有模型都能正常工作。但是,其中一个类的管理视图总是失败,并显示错误,“DatabaseError at / admin / profiles / address / 没有这样的表:profiles_address。“
我的个人资料应用程序有两个模型类。
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.ForeignKey(User)
stripe_id = models.CharField(max_length=300)
timestamp = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now_add=False, auto_now=True)
def __unicode__(self, ):
return str(self.user)
class Address(models.Model):
user = models.ForeignKey(User)
nickname = models.CharField(max_length=120, null=True, blank=True)
address1 = models.CharField(max_length=300)
address2 = models.CharField(max_length=300, null=True, blank=True)
city = models.CharField(max_length=300)
state = models.CharField(max_length=300)
country = models.CharField(max_length=300)
postal_code = models.CharField(max_length=300)
default_address = models.BooleanField(default=False)
billing_address = models.BooleanField(default=False)
shipping_address = models.BooleanField(default=True)
timestamp = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now_add=False, auto_now=True)
def __unicode__(self, ):
return self.address1
admin.py文件如下所示:
from django.contrib import admin
from .models import Address, Profile
class ProfileAdmin(admin.ModelAdmin):
class Meta:
model = Profile
admin.site.register(Profile, ProfileAdmin)
class AddressAdmin(admin.ModelAdmin):
class Meta:
model = Address
admin.site.register(Address, AddressAdmin)
当我对表进行查询时,它存在:
$ python manage.py sql profiles
BEGIN;
CREATE TABLE "profiles_profile" (
"id" integer NOT NULL PRIMARY KEY,
"user_id" integer NOT NULL REFERENCES "auth_user" ("id"),
"stripe_id" varchar(300) NOT NULL,
"timestamp" datetime NOT NULL,
"update" datetime NOT NULL
)
;
CREATE TABLE "profiles_address" (
"id" integer NOT NULL PRIMARY KEY,
"user_id" integer NOT NULL REFERENCES "auth_user" ("id"),
"nickname" varchar(120),
"address1" varchar(300) NOT NULL,
"address2" varchar(300),
"city" varchar(300) NOT NULL,
"state" varchar(300) NOT NULL,
"country" varchar(300) NOT NULL,
"postal_code" varchar(300) NOT NULL,
"default_address" bool NOT NULL,
"billing_address" bool NOT NULL,
"shipping_address" bool NOT NULL,
"timestamp" datetime NOT NULL,
"update" datetime NOT NULL
)
;
管理员中的视图显示“个人档案”表,但是当我转到“地址”管理员视图时,它会抛出错误。
有关正在发生的事情的任何想法?我能找到的其他答案似乎都没有这个问题/解决方案,除非我只是太难理解了。感谢。
----- ------ UPDATE 感谢Peter在评论中。一旦我意识到数据库不正确,我就可以进入实际的表并编辑数据库。我尝试使用dbshell选项,但它似乎在学习上有一些开销。我绝对推荐SQLite Studio。效果很好,消除了命令行的开销。如果你使用它我也会建议捐赠。