Я пытаюсь загрузить данные из json файла в django модели, которую я пробовал, но не удалось загрузить все данные в моей django модели администратора, и вот мой код, который я пробовал до сих пор, и вот мой json файл из я хочу сохранить данные в django модель http://www.mocky.io/v2/5e5bce20300000745ef9f2bb. я хочу загрузить все данные в моей django модели администратора из json файла.
class CronaVirus(models.Model):
timestamp = models.DateTimeField(auto_now_add=True)
currentConfirmedCount = models.IntegerField(
blank=True, null=True, help_text="Number of current remaining confirmed patients ")
confirmCount = models.IntegerField(
blank=True, null=True, help_text="Number of confirmed patients ")
suspectedCount = models.IntegerField(
blank=True, null=True, help_text="Number of suspected patients ")
curedCount = models.IntegerField(
blank=True, null=True, help_text="Number of cured patients ")
deadCount = models.IntegerField(
blank=True, null=True, help_text="Number of dead patients ")
seriousCount = models.IntegerField(
blank=True, null=True, help_text="Number of serious patients ")
currentConfirmedIncr = models.IntegerField(
blank=True, null=True, help_text="Number of current remaining confirmed patients (increased from yesterday)")
confirmIncr = models.IntegerField(
blank=True, null=True, help_text="Number of confirmed patients (increased from yesterday)")
suspectedIncr = models.IntegerField(
blank=True, null=True, help_text="Number of suspected infection (increased from yesterday)")
curedIncr = models.IntegerField(
blank=True, null=True, help_text="Number of cured patients (increased from yesterday)")
seriousIncr = models.IntegerField(
blank=True, null=True, help_text="Number of critically ill (increased from yesterday)")
deadIncr = models.IntegerField(
blank=True, null=True, help_text="Number of death (increased from yesterday)")
updateTime = models.IntegerField(
blank=True, null=True, help_text="The latest updated time")
note1 = models.CharField(max_length=255, blank=True,
null=True, help_text="Name of the virus")
note2 = models.CharField(max_length=255, blank=True,
null=True, help_text="Source of infection")
note3 = models.CharField(max_length=255, blank=True,
null=True, help_text="Way of spreading")
def __str__(self):
return self.note1
url = 'http://www.mocky.io/v2/5e5bce20300000745ef9f2bb'
r = requests.get(url)
titles = r.json()
# for title in titles:
# CronaVirus.objects.create(
# currentConfirmedCount=title['currentConfirmedCount'],
# confirmCount=title['confirmedCount'],
# suspectedCount=title['suspectedCount'],
# curedCount=title['curedCount'],
# deadCount=title['deadCount'],
# seriousCount=title['seriousCount'],
# currentConfirmedIncr=title['currentConfirmedIncr'],
# confirmIncr=title['confirmedIncr'],
# suspectedIncr=title['suspectedIncr'],
# curedIncr=title['curedIncr'],
# seriousIncr=title['seriousIncr'],
# deadIncr=title['deadIncr'],
# updateTime=title['updateTime'],
# note1=title['note1'],
# note2=title['note2'],
# note3=title['note3'])
# # Then pass this dict below as the template context
# context = {'titles': CronaVirus.objects.all()}
@classmethod
def create(cls, **kwargs):
cronavirus = cls.objects.create(
currentConfirmedCount=kwargs['currentConfirmedCount'],
confirmCount=kwargs['confirmedCount'],
suspectedCount=kwargs['suspectedCount'],
curedCount=kwargs['curedCount'],
deadCount=kwargs['deadCount'],
seriousCount=kwargs['seriousCount'],
currentConfirmedIncr=kwargs['currentConfirmedIncr'],
confirmIncr=kwargs['confirmedIncr'],
suspectedIncr=kwargs['suspectedIncr'],
curedIncr=kwargs['curedIncr'],
seriousIncr=kwargs['seriousIncr'],
deadIncr=kwargs['deadIncr'],
updateTime=kwargs['updateTime'],
note1=kwargs['note1'],
note2=kwargs['note2'],
note3=kwargs['note3'],
)
return cronavirus