在我的视频模型上,我已将User设置为外键。我正在使用默认的django auth用户模型,并设法使用序列化器创建了一个用户并查看了我为此设置的视图。问题是,每当我尝试创建视频条目时,都会出现标题错误。
我已经尝试清除数据库,重做迁移,但是处理用户序列化程序的应用程序没有任何迁移。
模型
from django.contrib.auth.models import User
class Video(models.Model):
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
null=True
)
map_marker = models.ForeignKey(MapMarker, on_delete=models.CASCADE)
created = models.DateTimeField()
ip_addr = models.TextField()
file_id = models.TextField()
class Meta:
ordering = ('created',)
完整追溯:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/videos/
Django Version: 2.2.1
Python Version: 3.7.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'userbase',
'trickspot',
'profanity']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\django\db\backends\utils.py" in _execute
84. return self.cursor.execute(sql, params)
The above exception (column "user_id" of relation "trickspot_video" does not exist
LINE 1: INSERT INTO "trickspot_video" ("user_id", "map_marker_id", "...
^
) was the direct cause of the following exception:
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
54. return view_func(*args, **kwargs)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\rest_framework\viewsets.py" in view
116. return self.dispatch(request, *args, **kwargs)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\rest_framework\views.py" in dispatch
495. response = self.handle_exception(exc)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\rest_framework\views.py" in handle_exception
455. self.raise_uncaught_exception(exc)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\rest_framework\views.py" in dispatch
492. response = handler(request, *args, **kwargs)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\rest_framework\mixins.py" in create
21. self.perform_create(serializer)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\rest_framework\mixins.py" in perform_create
26. serializer.save()
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\rest_framework\serializers.py" in save
214. self.instance = self.create(validated_data)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\rest_framework\serializers.py" in create
943. instance = ModelClass._default_manager.create(**validated_data)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\django\db\models\manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\django\db\models\query.py" in create
422. obj.save(force_insert=True, using=self.db)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\django\db\models\base.py" in save
741. force_update=force_update, update_fields=update_fields)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\django\db\models\base.py" in save_base
779. force_update, using, update_fields,
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\django\db\models\base.py" in _save_table
870. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\django\db\models\base.py" in _do_insert
908. using=using, raw=raw)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\django\db\models\manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\django\db\models\query.py" in _insert
1186. return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
1335. cursor.execute(sql, params)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\django\db\backends\utils.py" in execute
99. return super().execute(sql, params)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\django\db\backends\utils.py" in execute
67. return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\django\db\backends\utils.py" in _execute_with_wrappers
76. return executor(sql, params, many, context)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\django\db\backends\utils.py" in _execute
84. return self.cursor.execute(sql, params)
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\django\db\utils.py" in __exit__
89. raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Zeph\.virtualenvs\SK8M8_API-LRdA6z3-\lib\site-packages\django\db\backends\utils.py" in _execute
84. return self.cursor.execute(sql, params)
Exception Type: ProgrammingError at /videos/
Exception Value: column "user_id" of relation "trickspot_video" does not exist
LINE 1: INSERT INTO "trickspot_video" ("user_id", "map_marker_id", "...
^
该条目应与我在API视图上选择的用户的密钥一起保存。
答案 0 :(得分:0)
在此过程中发生了一些问题,删除了数据库并重新开始解决了该问题。
谢谢