我在 django 多表继承中创建了一对一的关系,但它没有在我的子类表中显示 Book-id,并且还显示完整性错误,为什么?
这是我的 models.py :
class Book(models.Model):
Book_Name = models.CharField(max_length=100, default="Don Quixote")
Author_Name = models.CharField(max_length=100, blank=True, default="Miguel de Cervantes")
Price = models.FloatField(default=175.00)
created = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now=True)
def __str__(self):
return self.Book_Name
class ISBN(models.Model):
book = models.OneToOneField(Book, on_delete=models.CASCADE, parent_link=True, primary_key=True)
Isbn = models.CharField(max_length=100)
def __str__(self):
return self.Book_Name
根据我的代码,每本书都有一个 isbn 编号,但在管理面板 ISBN 表中不显示书籍属性或不显示任何书籍 ID。它只显示 Isbn 列,在我在此列中输入输入后,它显示错误“IntegrityError at /admin/inheritance/isbn/add/”和“NOT NULL 约束失败:inheritance_isbn.book_id”
这是我的 admin.py :
@admin.register(Book)
class PostAdmin(admin.ModelAdmin):
list_display = ['Book_Name', 'Author_Name', 'Price']
@admin.register(ISBN)
class PostAdmin(admin.ModelAdmin):
list_display = ['book', 'Isbn']
我'我是 python 和 Django 的菜鸟,所以任何帮助将不胜感激。
This is my models.py :
class Book(models.Model):
Book_Name = models.CharField(max_length=100, default="Don Quixote")
Author_Name = models.CharField(max_length=100, blank=True, default="Miguel de Cervantes")
Price = models.FloatField(default=175.00)
created = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now=True)
def __str__(self):
return self.Book_Name
class ISBN(models.Model):
book = models.OneToOneField(Book, on_delete=models.CASCADE, parent_link=True, primary_key=True)
Isbn = models.CharField(max_length=100)
def __str__(self):
return self.Book_Name
in according to my code every Book has one isbn number but in admin panel ISBN table don't show book attributes or don't show any book-id. it show only Isbn column and after i give input in this column it show me error "IntegrityError at /admin/inheritance/isbn/add/" and "NOT NULL constraint failed: inheritance_isbn.book_id"
This is my admin.py :
@admin.register(Book)
class PostAdmin(admin.ModelAdmin):
list_display = ['Book_Name', 'Author_Name', 'Price']
@admin.register(ISBN)
class PostAdmin(admin.ModelAdmin):
list_display = ['book', 'Isbn']
I'm a noob to python and Django so any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在
PostAdmin.list_display
上更改book
的book__id
Try changing
book
onPostAdmin.list_display
forbook__id