Django 一对多模型
以下模型描述了漏洞以及 Internet 上引用该漏洞的 URL。假设每个 URL 只讨论 1 个漏洞,并且许多 URL 都会讨论该漏洞。这是布局模型的正确方法吗?
class Vuln(models.Model):
pub_date = models.DateTimeField("Publication Date")
short_description = models.CharField("Description", max_length=70)
reference_urls = models.ForeignKey(Url, unique=True, blank=True, verbose_name="Reference URLs")
vendor = models.ForeignKey(Vendor, verbose_name="Vendor")
class Url(models.Model):
url = models.URLField("URL", max_length=200)
管理应用程序为参考 URL 提供了一个“选择”框,这不是我想要的。当我添加新的漏洞对象时,已输入的所有现有 URL 都会显示在该下拉列表中,这又不自然。我觉得这应该与博客评论非常相似,即。该评论适用于单个博客条目,而不是其他条目,并且一个博客条目可能有许多评论。我如何在 Django 模型中表达这一点?
The following models describe a vulnerability and the URLs out on the internet that reference that vulnerability. Assume that each URL only ever talks about 1 vulnerability, and that many URLs will discuss that vulnerability. Is this the correct way to lay out the model?
class Vuln(models.Model):
pub_date = models.DateTimeField("Publication Date")
short_description = models.CharField("Description", max_length=70)
reference_urls = models.ForeignKey(Url, unique=True, blank=True, verbose_name="Reference URLs")
vendor = models.ForeignKey(Vendor, verbose_name="Vendor")
class Url(models.Model):
url = models.URLField("URL", max_length=200)
The Admin application gives a 'select' box for the reference URLs, which isn't what I want. When I add a new vulnerability object, all of the existing URLs that have been entered show up in that dropdown, which is again unnatural. I feel like this should behave very similar to how a blog comment would, ie. the comment applies to a single blog entry and none other and that one blog entry may have many comments. How do I express this in a Django model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它应该更像是这样的:
如果你说每个 Url 都讨论一个特定的漏洞,那么 Django DBM 中就有你的关系:)
至于供应商字段,你只需添加另一个类,就像 Vuln 类一样。例如:
希望这有帮助!
问候,亚历克斯
It should be more like this:
If you're saying each Url talks about a specific vulnerability, then there is your relation in the Django DBM :)
As for the vendor field, you simply add another class, much like Class Vuln. For example:
Hope this helps!
Regards, Alex