' __代理__'对象没有属性' get'在CreateView中

时间:2016-06-04 06:02:42

标签: python django django-views

所以我认为这不是正确的做事方式,但我正在努力学习django而我正在尝试一些事情。我试图通过在Formula的实例中进行硬编码来为我的maker模型设置外键。

模特:

class Cooker(models.Model):
    name = models.CharField(max_length=20, name="name")
    background = models.CharField(max_length=500, name="background")


class Formula(models.Model):
    food = models.CharField(max_length=200, name="food")
    maker = models.ForeignKey(Cooker, related_name="cooker_key")

浏览

class CookerCreate(CreateView):
    template_name = "cookercreate.html"
    model = Cooker
    fields = ['name','background']
    success_url = reverse_lazy('cooker')

class FormulaCreate(CreateView):
    template_name = "formulahome.html"
    model = Formula
    fields = ['food']
    success_url = reverse_lazy('formulahome')

    def form_valid(self, form):
        self.object = form.save(commit = False)
        self.object.maker = Cooker.objects.get(pk=1)
        form.save()
        return reverse_lazy('formula home')

在我设置FormulaCreate的{​​{1}}课程中,我只想在我已经创建的self.object.maker中进行硬编码。感谢

编辑:当我尝试在Cooker中提交表单时,我收到错误FormulaCreate(CreateView)

1 个答案:

答案 0 :(得分:3)

您的错误原因是pre | <!DOCTYPE html> | foo bar | </html> 应该返回form_valid个对象,并且您要返回一个URL。

不要手动执行此操作,而应调用父方法,该方法将重定向到您已定义的Response

success_url