CreateView 抛出“DoesNotExist”当未向表单提供实例时
我在进行以下设置时收到“DoesNotExist”错误 - 我已经尝试调试了一段时间,但无法弄清楚。
class Video(models.Model):
name = models.CharField(max_length=100)
type = models.CharField(max_length=100)
owner = models.ForeignKey(User, related_name='videos')
...
#Related m2m fields
....
class VideoForm(modelForm):
class Meta:
model = Video
fields = ('name', 'type')
class VideoCreate(CreateView):
template_name = 'video_form.html'
form_class = VideoForm
model = Video
当我执行此操作并发布“名称”和“类型”数据时 - 我收到“DoesNotExist”错误。它似乎与 UpdateView 一起工作正常 - 或者当传递“实例”来初始化表单时。
这是引发错误的确切位置: /usr/lib/pymodules/python2.7/django/db/models/fields/lated.py 在 get 中,第 301 行
有谁知道可能发生了什么?
谢谢
I'm getting a "DoesNotExist" error with the following set up - I've been trying to debug for a while and just can't figure it out.
class Video(models.Model):
name = models.CharField(max_length=100)
type = models.CharField(max_length=100)
owner = models.ForeignKey(User, related_name='videos')
...
#Related m2m fields
....
class VideoForm(modelForm):
class Meta:
model = Video
fields = ('name', 'type')
class VideoCreate(CreateView):
template_name = 'video_form.html'
form_class = VideoForm
model = Video
When I do this and post data for 'name' and 'type' - I get a "DoesNotExist" error. It seems to work fine with an UpdateView - or when an 'instance' is passed to init the form.
This is the exact location where the error is raised:
/usr/lib/pymodules/python2.7/django/db/models/fields/related.py in get, line 301
Does anyone know what might be going on?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您尚未发布完整的回溯,我的猜测是您的所有者 FK 不是可选的,并且您没有在模型表单中指定一个。
您需要发布完整的回溯。
Since you have not posted your full traceback, my guess is that your owner FK is not optional, and you are not specifying one in your model form.
You need to post a full traceback.
我认为它必须是类
VideoForm(ModelForm)
而不是VideoForm(modelForm)
。如果您不打算在表单中使用外键,请使用
exclude = ('owner')
I think it has to be class
VideoForm(ModelForm)
instead ofVideoForm(modelForm)
.If you aren't going to use the foreign key in the form use
exclude = ('owner')