I'm quite new to Django and would like to clear a noob doubt. Say I have a post request to a model Scans
in django.
The json data for the same looks like
[{"id":1, "results":{"low": 3, "medium": 6, "high": 7}, "machine": "Windows", "report": "yes"]
How should the model look like? I can't figure out what field type I should give to results
.
Try out JSONField . Your model should look like that.
from django.contrib.postgres.fields import JSONField
from django.db import models
class Scans(models.Model):
machine = models.CharField(...)
results = JSONField(default=dict)
PS You need to configure your database(in this case PostgreSQL)
PSS If you dont want to use PostgreSQL here is JSONFIeld for MySQL
You can also create two models Scans
and Results
and create a relationship between them as follows
class Scans(models.Model):
machine = models.CharField(max_length = 100)
# assuming that report is by default false
report = models.BooleanField(default = False)
results = models.ForeignKey(Results, on_delete = models.CASCADE)
class Results(models.Model):
low = models.IntegerField()
medium = models.IntegerField()
high = models.IntegerField()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.