CreateView 抛出“DoesNotExist”当未向表单提供实例时

发布于 2024-12-24 03:16:52 字数 759 浏览 1 评论 0原文

我在进行以下设置时收到“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

远昼 2024-12-31 03:16:52

由于您尚未发布完整的回溯,我的猜测是您的所有者 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.

清眉祭 2024-12-31 03:16:52

我认为它必须是类 VideoForm(ModelForm) 而不是 VideoForm(modelForm)

如果您不打算在表单中使用外键,请使用 exclude = ('owner')

I think it has to be class VideoForm(ModelForm) instead of VideoForm(modelForm).

If you aren't going to use the foreign key in the form use exclude = ('owner')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文