下面是我的Django模型代码
from django.db import models
class BookUser(models.Model):
email= models.CharField(max_length=254,primary_key=True) #mail address key
name = models.CharField(max_length=254) #max 64 char (lower case?)
contact= models.CharField(max_length=12)
imei = models.CharField(max_length=16) #imei number
address= models.TextField() #list of address ids
booksInShelf:[] #list of user book's unique ids
booksUnderCirculation:[] #list of user book's unique ids
class Meta:
ordering = ('email',)
class Book(models.Model):
isbn = models.CharField(max_length=13)
title=models.CharField(max_length=500)
description =models.TextField()
author = models.CharField(max_length=200)
userRating = models.CharField(max_length=1)
users = #list of user ids hold this book in shelf
class UserBook(models.Model):
#id: generated by django
bookId: #id of parent book
rent= models.BooleanField(default=False) #boolean is ready to rent
sell= models.BooleanField(default=False) #boolean is ready to sell
price =models.FloatField() #selling price
rentBase=models.FloatField() #base price of rent
rentPeriod=models.IntegerField() #days after which extra rent would apply
dateModified =models.DateTimeField(auto_now=True) #track date it came into shelf
dateAdded = models.DateTimeField(auto_now_add=True)
这里BookUser是实际用户,有两个类别的书籍,即booksinShelf和bookUnderCirculation
class Book是所有书籍的中央存储库,我需要定义与BookUser的一对多关系。这是一种简单的方法吗?
用户手册特定于BookUser,它应该唯一地指向Class Book,因此它与Book Class的多对一关系。
我对如何处理UserBook和Book的ID感到困惑? 另外如何在BookUser类中存储UserBook的ID列表?
答案 0 :(得分:0)
这应该做你想要的:
class UserBook(models.Model):
bookId = models.ForeignKey('Book')
此处UserBook
引用了Book
项,并且多个用户可以拥有相同的图书,但它仍然是您的图书集中的唯一参考。
希望有所帮助