Django:维护单独的对象集,几乎就像“孩子”一样
我目前正在掌握 Django 的一些优点,并开始认真使用它。我有一个示例,其中有一些外键关系,并且我想将对象分开。这是一个快速的 models.py 示例:
class CopyOfBook(models.Model):
barcode = models.CharField(max_length=100)
class Book(models.Model):
title = models.CharField(max_length=100)
copies = models.ManyToManyField(CopyOfBook)
class Library(models.Model):
name = models.CharField(max_length=100)
books = models.ManyToManyField(Book)
每个组件都依赖于其父组件 - 例如,一本书不能存在于库之外,并且特定于单个库。我知道这是一个稍微做作的示例,但是在 Django 中对此进行建模的最佳方法是什么?
问题是,如果您打开管理并添加一本新书,您将看到所有 CopyOfBook 实例的列表,而不仅仅是属于该书的实例(当添加新书时,将是一个空列表)。
希望我已经解释得足够清楚了,如果您需要任何说明,请告诉我。
更新:我一直在尝试使用ManyToMany.limit_choices_to
,并在参数中使用适当的lated_name
,但没有太多乐趣。
I'm currently getting to grips with some of the finer points in Django, and I'm beginning to use it in earnest. I've got an example where I have a few foreign key relationships, and I want to keep the objects separate. Here's a quick models.py
example:
class CopyOfBook(models.Model):
barcode = models.CharField(max_length=100)
class Book(models.Model):
title = models.CharField(max_length=100)
copies = models.ManyToManyField(CopyOfBook)
class Library(models.Model):
name = models.CharField(max_length=100)
books = models.ManyToManyField(Book)
Each of those components depends on its parent - for example, a Book can't exist outside of a Library, and is specific to a single library. I know it's a slightly contrived example, but what's the best way to model this in Django?
The problem is, that if you opened up the admin and added a new Book, you'd be presented with a list of all the CopyOfBook instances, rather than just those that belong to that book (which when adding a new one, would be an empty list).
Hopefully I've explained that clearly enough, let me know if you need any clarification.
Update: I have been trying to use ManyToMany.limit_choices_to
, and using the appropriate related_name
in the arguments, but without much joy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该首先考虑您的实体关系模型。
CopyOfBook 有一个书和一个库。所以你可以给每个它一个外键。
Book 和 Library 仅通过 CopyOfBook 连接。因此,虽然这不是绝对必要的(上面两个外键就足够了),但您可以使用 through=CopyOfBook 为 Library 提供与 Book 的多对多关系。
You should start by thinking of your entity-relationship model.
A CopyOfBook has a single Book and a single Library. So you can give it a ForeignKey to each.
Book and Library are only connected through a CopyOfBook. So while this isn't strictly necessary (the two above ForeignKey suffice), you can give Library a ManyToMany relationship to Book with through=CopyOfBook.
如果一本书特定于单个库,您应该在其上添加一个
ForeignKey
到 Library,而不是在Library
中添加一个ManyToManyField
。另外,我认为CopyOfBook
也将特定于一本书,所以这同样适用于这里!然后,您可以通过 InlineModelAdmins 管理书籍和副本!
If a book is specific to a SINGLE library you should rather have a
ForeignKey
to Library on it, than aManyToManyField
inLibrary
. Also I suppose aCopyOfBook
will also be specific for a single book, so the same applies here!You could then administrate the books and copies via InlineModelAdmins!
Library
和Book
之间的ManyToMany
关系很好,因为多个图书馆可以拥有同一本书。但是,Book
和CopyOfBook
之间的ManyToMany
关系对我来说没有意义。特定副本可以是特定书籍的。相反,您应该拥有一种多对一 (ForeignKey)
关系,它将一本特定的书与其多个副本相关联。The
ManyToMany
relationship that you have betweenLibrary
andBook
is fine, because more than one library can have the same book. However, theManyToMany
relationship that you have betweenBook
andCopyOfBook
does not make sense to me. A particular copy can be of a particular book. What you should have there instead is amany-to-one (ForeignKey)
relationship, which will associate a particular book with more than one copies of it.