Python 3.x django模型实例上的外键约束失败
我尝试在一个动作上创建一个模型实例。我无法让这个过程成功。我收到一条关于外键Constraint失败的错误消息,我被卡住了。 下面是我尝试为模型返回创建模型实例的视图Python 3.x django模型实例上的外键约束失败,python-3.x,django,django-models,django-views,Python 3.x,Django,Django Models,Django Views,我尝试在一个动作上创建一个模型实例。我无法让这个过程成功。我收到一条关于外键Constraint失败的错误消息,我被卡住了。 下面是我尝试为模型返回创建模型实例的视图 @login_required(login_url='/login/') def return_book(request,pk): books = Books.objects.filter(school = request.user.school).get(id = pk) if books: Re
@login_required(login_url='/login/')
def return_book(request,pk):
books = Books.objects.filter(school = request.user.school).get(id = pk)
if books:
Return.objects.create(borrowed_item_id=books.id,returner_id=request.user.school.id)
return redirect("view_books")
模型
class Books(models.Model):
school = models.ForeignKey(School, on_delete=models.CASCADE)
reg_no = models.CharField(max_length=50)
book_name = models.CharField(max_length=200)
class Issue(models.Model):
borrower_id = models.ForeignKey(Student,on_delete=models.CASCADE)
book_id = models.ForeignKey(Books,on_delete=models.CASCADE)
class Return(models.Model):
return_date = models.DateField(default=datetime.date.today)
borrowed_item = models.ForeignKey(Issue,on_delete=models.DO_NOTHING)
returner = models.ForeignKey(CustomUser,on_delete=models.DO_NOTHING)
还有追踪
Traceback (most recent call last):
File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "D:\Python\Django\test projects\library manage\lib_system\Library-System\libman\views.py", line 216, in return_book
Return.objects.create(borrowed_item_id=books.id,returner_id=request.user.id)
File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\query.py", line 447, in create
obj.save(force_insert=True, using=self.db)
File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\base.py", line 753, in save
self.save_base(using=using, force_insert=force_insert,
File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\base.py", line 790, in save_base
updated = self._save_table(
File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\base.py", line 895, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\base.py", line 933, in _do_insert
return manager._insert(
File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\query.py", line 1254, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\sql\compiler.py", line 1397, in execute_sql
cursor.execute(sql, params)
File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\utils.py", line 98, in execute
return super().execute(sql, params)
File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\sqlite3\base.py", line 413, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: FOREIGN KEY constraint failed
这是postgresql发布的错误
insert or update on table "libman_return" violates foreign key constraint "libman_return_borrowed_item_id_b84dde78_fk_libman_issue_id"
DETAIL: Key (borrowed_item_id)=(9) is not present in table "libman_issue".
这是完整的回溯吗?回溯中至少应该有一行代码指向您的代码(在您显示的内容中没有)。请显示完整的回溯。完整的回溯已经更新…
returner\u id=request.user.school.id
,这里我看到returner
是CustomUser
的外键,但是request.user.school.id
意味着它是一些模型实例的id,比如school
(猜猜这里的名字)?也许您需要编写returner\u id=request.user.id
?它没有显示任何差异。答案是一样的。啊,借来的项目
是问题
的外键,但是你给它传递了书籍
的id(注意:模型名称最好是单数的,因此书籍
应该是一个更好的名称)例如,看到问题了吗?