我的英语很差,抱歉
这是我的结构:
bookstore
---author(app1)
---book(app2)
或者在代码中:
from django.db import models
from author.models import Profile
from django.contrib import admin
class Book(models.Model):
title = models.CharField(max_length = 150)
page = models.IntegerField()
price = models.IntegerField()
author = models.ForeignKey(
'Profile',
on_delete=models.CASCADE,)
publish_date = models.DateField()
class Meta(object):
db_table = "book"
class BookAdmin(admin.ModelAdmin):
pass
admin.site.register(Book, BookAdmin)
mysql 有一些数据,现在我想用它们来显示我的网页,而不是在数据库中创建数据。谢谢大家!
我有一个问题:
我的 Django == 1.9,python == 3,windows10
我想用mysql(我的数据库概念确实是这样做的)。
当我找到一些资源时,我会看到
python manage.py sql [appname]
它是 Django 到 SQL
当我想使用Django到mysql时。
我可以使用 python manage.pyspectdb 吗?它将有一个 models.py
python manage.py sql [应用程序名称]
= python manage.pyspectdb
?
错误:
book.Book.author: (fields.E300) Field defines a relation with model 'Profile', which is either not installed, or is abstract.
book.Book.author: (fields.E307) The field book.Book.author was declared with a lazy reference to 'book.profile', but app 'book' doesn't provide model 'profile'.
最佳答案
在您的 Book
中模型,您使用名为 author
的字段来引用到模型 Profile
。由于该模型是在另一个应用程序中定义的,因此您应该将其称为 app_name.ModelName
,很可能是:
class Book(models.Model):
title = models.CharField(max_length = 150)
page = models.IntegerField()
price = models.IntegerField()
author = models.ForeignKey(
<b>'app1.Profile'</b>, # add name of the app
on_delete=models.CASCADE,
)
publish_date = models.DateField()
如果您将此模型命名为 Author
但是,正如问题文本(而不是代码)似乎表明的那样,您应该使用 app1.Author
。当然你替换app1
使用应用程序的真实名称。
ForeignKey
[Django-doc] 中的文档对此进行了描述:
To refer to models defined in another application, you can explicitly specify a model with the full application label. For example, if the Manufacturer model above is defined in another application called production, you’d need to use:
class Car(models.Model): manufacturer = models.ForeignKey( 'production.Manufacturer', on_delete=models.CASCADE, )
关于python - 错误 : book. 书.作者 : (fields. E300) 书.书.作者 : (fields. E307),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56972174/