Django 一对多模型

发布于 2024-08-17 14:02:52 字数 657 浏览 4 评论 0原文

以下模型描述了漏洞以及 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞清仓 2024-08-24 14:02:52

它应该更像是这样的:

class Vuln(models.Model): 
  pub_date = models.DateTimeField("Publication Date") 
  short_description = models.CharField("Description", max_length=70)
  vendor = models.ForeignKey(Vendor, verbose_name="Vendor") 

class Url(models.Model): 
  url = models.URLField("URL", max_length=200)
  vulnerability = models.ForeignKey(Vuln)

如果你说每个 Url 都讨论一个特定的漏洞,那么 Django DBM 中就有你的关系:)

至于供应商字段,你只需添加另一个类,就像 Vuln 类一样。例如:

class Vendor(models.Model): 
  field_names_go_here = models.TextField(max_length=70)
  short_description = models.CharField("Description", max_length=70)

希望这有帮助!
问候,亚历克斯

It should be more like this:

class Vuln(models.Model): 
  pub_date = models.DateTimeField("Publication Date") 
  short_description = models.CharField("Description", max_length=70)
  vendor = models.ForeignKey(Vendor, verbose_name="Vendor") 

class Url(models.Model): 
  url = models.URLField("URL", max_length=200)
  vulnerability = models.ForeignKey(Vuln)

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:

class Vendor(models.Model): 
  field_names_go_here = models.TextField(max_length=70)
  short_description = models.CharField("Description", max_length=70)

Hope this helps!
Regards, Alex

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文