Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django One-To-Many Models

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?

like image 206
Dan Avatar asked Jan 11 '10 04:01

Dan


1 Answers

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

like image 164
Alex Avatar answered Oct 30 '22 20:10

Alex