Я скопировал файл json из сети. Я использую это для django проекта. Я ввел следующие строки в cmd -
>>> import json
>>> from blog.models import Post
>>> with open('posts.json') as f:
... posts_json=json.load(f)
...
>>> for post in posts_json:
... post = Post(title=post['title'], content=post['content'], author_id=post['
user_id'])
... post.save()
...
И я получил ошибку -
Traceback (most recent call last):
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\sqlite3\base.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: FOREIGN KEY constraint failed
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<console>", line 3, in <module>
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\base.py", line 745, in save
self.save_base(using=using, force_insert=force_insert,
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\base.py", line 782, in save_base
updated = self._save_table(
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\base.py", line 887, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields
, raw)
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\base.py", line 924, in _do_insert
return manager._insert(
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\query.py", line 1204, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\sql\compiler.py", line 1391, in execute_sql
cursor.execute(sql, params)
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._e
xecute)
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\sqlite3\base.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: FOREIGN KEY constraint failed
Это мой файл models.py -
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
class Post(models.Model):
title=models.CharField(max_length=100)
content=models.TextField()
date_posted=models.DateTimeField(default=timezone.now)
author=models.ForeignKey(User,on_delete=models.CASCADE)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail',kwargs={'pk':self.pk})
Это файл json, который я скопировал из Интернета https://raw.githubusercontent.com/CoreyMSchafer/code_snippets/master/Django_Blog/snippets/posts.json