Creates models for the DB - #13
Conversation
Signed-off-by: Thomas Druez <tdruez@nexb.com>
Signed-off-by:Kartik Sibal <kartiksibal@gmail.com>
Signed-off-by: Kartik Sibal <kartiksibal@gmail.com>
…nto django_app
Signed-off-by: Kartik Sibal <kartiksibal@gmail.com>
Signed-off-by: Kartik Sibal <kartiksibal@gmail.com>
|
|
||
|
|
||
| class VulnerabilityReference(models.Model): | ||
| vulnerability_id = models.ForeignKey('Vulnerability') |
There was a problem hiding this comment.
when you use a FK here, do not use _id. What you get is an object, not an ID. Also reference the model as a class here, not as a string. This applies to all the models here.
There was a problem hiding this comment.
@pombredanne I read it here: https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.ForeignKey
Could you please explain a bit, as to why in the documentation it is referenced as a string?
There was a problem hiding this comment.
https://docs.djangoproject.com/en/1.11/ref/models/fields/#database-representation
Behind the scenes, Django appends "_id" to the field name to create its database column name. In the above example, the database table for the Car model will have a manufacturer_id column. (You can change this explicitly by specifying db_column) However, your code should never have to deal with the database column name, unless you write custom SQL. You’ll always deal with the field names of your model object.
Simple use: vulnerability = models.ForeignKey('Vulnerability')
There was a problem hiding this comment.
@tdruez, Could you maybe explain @pombredanne ' point of view?
| vulnerability_id = models.ForeignKey('Vulnerability') | ||
| source = models.CharField(max_length=50) | ||
| reference_id = models.CharField(max_length=50) | ||
| url = models.URLField(max_length=50) |
There was a problem hiding this comment.
When did you last see a URL that's 50 char long?
There was a problem hiding this comment.
@pombredanne I didn't understand your tone here. Too long or short? 😛
There was a problem hiding this comment.
50 is way to short, 1024 should be plenty.
|
|
||
| class VulnerabilityReference(models.Model): | ||
| vulnerability_id = models.ForeignKey('Vulnerability') | ||
| source = models.CharField(max_length=50) |
There was a problem hiding this comment.
Please add a help to each field that documents them.
There was a problem hiding this comment.
@pombredanne is referring to https://docs.djangoproject.com/en/1.11/ref/models/fields/#help-text
This is a great way to document the purpose of each field. This will also be re-used in the admin and API documentation.
|
|
||
|
|
||
| class Package(models.Model): | ||
| package_id = models.AutoField(primary_key=True) |
There was a problem hiding this comment.
This is not needed. We can use the implicit id instead.
We also need some unique together on (platform, name, version)
| # Create your models here. | ||
|
|
||
| class Vulnerability(models.Model): | ||
| vulnerability_id = models.AutoField(primary_key=True) |
There was a problem hiding this comment.
I wonder if we are going to need a vulnerability ID that's not an auto field I think.
There was a problem hiding this comment.
@pombredanne I had the same dilemma, my thought process was to primarily diff. package id from vulnerability id.
There was a problem hiding this comment.
You can remove this line. See https://docs.djangoproject.com/en/1.11/ref/models/fields/#primary-key
If you don’t specify primary_key=True for any field in your model, Django will automatically add an AutoField to hold the primary key
This apply to all models.
There was a problem hiding this comment.
@tdruez I did read that. My primary motive was to diff. vulnerability id from package id. Henc, I didn't use the default id. But, I'll change that. 👍
| package_id = models.AutoField(primary_key=True) | ||
| platform = models.CharField(max_length=50) | ||
| name = models.CharField(max_length=50) | ||
| version = models.FloatField(max_length=50) |
There was a problem hiding this comment.
You should use a CharField for all version type fields.
FloatField is not suitable for something like "1.2.3"
Signed-off-by: Kartik Sibal kartiksibal@gmail.com