问题标签 [modelform]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
8417 浏览

django - Django ModelForms - 'instance' not working as expected

I have a modelform that will either create a new model or edit an existing one - this is simple and should work, but for some reason I'm getting a new instance every time.

The scenario is this is the first step in an ecommerce order. The user must fill out some info describing the order (which is stored in the model). I create the model, save it, then redirect to the next view for the user to enter their cc info. I stick the model in the session so I don't have to do a DB lookup in the next view. There is a link in the template for the second (cc info) view that lets the user go back to the first view to edit their order.

If I step through in the debugger when I go back to the view to edit an order that the form is created with the instance set to the previously created model, as expected. However, when the form is processed in the subsequent POST, it creates a new instance of the model when form.save() is called.

I believe this is because I've restricted the fields in the form, so there is nowhere in the rendered HTML to store the id (or other reference) to the existing model. However, I tried adding both a 'pk' and an 'id' field (not at the same time), but then my form doesn't render at all.

I suspect I'm making this more complicated than it needs to be, but I'm stuck at the moment and could use some feedback. Thanks in advance.

0 投票
2 回答
958 浏览

python - Django 应用程序的本地化仅适用于 forms.py 而不适用于 models.py

尝试本地化我的应用程序时遇到问题。它有两种语言版本:英语和德语。当浏览器的语言设置为英语(美国)并且在我的设置文件中设置为“de”时,就会出现问题,反之亦然。有些字段以英语显示,有些则以德语显示。我的模型包含 CharField、DecimalField 和 DateField 字段类型。

模型.py:

表格.py:

设置.py

英语是浏览器设置的语言。字段 test_number 和 test_price 的标签显示为德语,而 test_date 的标签显示为英语。如果我从 models.py 中删除 _('Test number') 并将其添加为 forms.py 中的标签属性,它就可以工作。这不是另一种方式吗?

0 投票
2 回答
3920 浏览

django - 为什么 Django 不将我的 unique_together 约束强制为 form.ValidationError 而不是抛出异常?

编辑:虽然这篇文章是Django 的 ModelForm unique_together validation 的副本,但这里接受的从 ModelForm 中删除“排除”的答案比其他问题中接受的答案要干净得多。

这是对这个问题的跟进。

如果我没有明确检查 clean_title() 函数中的 unique_together 约束,django 会抛出异常:

/journal/journal/4 处的 IntegrityError

重复键值违反唯一约束“journal_journal_owner_id_key”

请求方法:POST

请求网址: http://localhost:8000/journal/journal/4

异常类型:IntegrityError

异常值:重复键值违反唯一约束“journal_journal_owner_id_key”

异常位置:/Library/Python/2.6/site-packages/django/db/backends/util.py 在执行中,第 19 行

但是,我的印象是 Django 会通过引发 ValidationError 很好地强制执行此约束,而不是我需要捕获的异常。

下面是我的代码,其中包含我用作解决方法的附加 clean_title() 方法。但我想知道我做错了什么,以至于 django 没有以预期的方式执行约束。

谢谢。

型号代码:

表格代码:

查看代码:

更新工作代码: 感谢 Daniel Roseman。

型号代码与上面相同。

表单代码 - 删除排除语句和 clean_title 函数:

查看代码 - 添加自定义唯一性错误消息:

0 投票
1 回答
1611 浏览

python - 如何覆盖 modelform 类的 save() 方法并添加缺失信息?

我刚开始学习 Django,我有一个问题。

我正在尝试在保存表单数据时自动添加缺失的信息。我可以通过覆盖 modelform 类的 save() 方法来更改/添加所需的“cleaned_data”信息,但更改不会记录在数据库中。其实,修改后的信息怎么写呢?这是代码:

和型号:

TIA

0 投票
1 回答
231 浏览

python - 验证 ModelForm 的问题

我使用 ModelForm 创建我的表单。除了一件事-验证唯一字段外,一切正常。代码:

但是当我用非唯一标题保存我的表单时,我没有收到我的自定义翻译错误,但我收到了默认错误。如何解决它,显示我的唯一字段错误?

编辑: 我认为,我发现这样做非常方便。也许有人会使用它:)

0 投票
1 回答
1572 浏览

python - django ModelForm save() 方法问题

我有一个模型形式:

我希望能够通过使用这个来编辑一个特定的实例:

请注意,在该行

form = SnippetForm(data=request.POST, instance=snippet)

,我创建了一个使用用户提供的数据的表单,并将其与使用主键(从 url 接收)找到的实例绑定。根据django 文档,当我调用 save() 时,应该使用 POSTED 数据更新现有实例。相反,我看到的是一个新对象被创建并保存到数据库中。什么地方出了错?非常感谢。

[编辑] 这真的很尴尬。该代码确实没有任何问题。唯一搞砸了整个事情的是我在模板中放入的操作(因为我使用相同的模板来添加和编辑片段)......非常感谢您的帮助,非常感谢。

0 投票
2 回答
4012 浏览

python - 无法在 __init__ 向 ModelForm 添加字段

我有一个问题ModelForm。显示字段test1,但test2不显示 -。玩base_fields没有帮助。

谢谢你。

0 投票
2 回答
2655 浏览

django - Django:同时保存多个模型表单(复杂情况)

好吧,这实际上可能是一个简单的案例,但我很难弄清楚。

我有两个用户注册渠道(公司和其他所有人)。当企业用户通过我的注册表单创建 User 实例时,我还希望他们输入创建相关实例的辅助表单(基于 Website 和 Organization_Contact 模型)。

我知道如何通过发出额外的同步或异步请求来解决这个问题,但我希望用户填写三个表单并一键提交。

我的问题是其他两种形式依赖于用户外键作为字段。我已经将该字段设置为“null=True,blank=True”,这样我就可以在没有外键的情况下验证和保存表单,但我最终想将该键添加到两个模型实例中。

我认为我可以验证这三个表单,保存 UserForm,然后使用模型查询集返回新的 User.id(全部在一个视图中)。然后,我将该 user_id 值添加到其他两个表单字典(WebsiteForm 和 Organization_ContactForm)。

它会这样工作:

问题:1)不确定我是否可以保存模型表单,然后在单个视图中将该模型实例作为查询集返回

2)我说我不确定,因为我无法通过尝试将变量传递给查询集的问题。

The get manager method seems to not accept a variable there. I assume as much because I passed an equivalent hardcoded string and it worked.

Ok, so I was thinking about creating a new manager method (email) which accepted a variable argument (the cleaned email field) and then retrying the process of saving one modelform, retrieving the model id data, and saving the other modelforms.

I also thought that I might be able to handle this issue through a post save signal.

Just general direction here would be really helpful. I need a starting point.

0 投票
4 回答
27136 浏览

django - 字段选择()作为查询集?

我需要制作一个表单,它有 1 个选择和 1 个文本输入。选择必须取自数据库。模型看起来像这样:

它的行仅由管理员添加,但所有用户都可以在表单中看到它。我想从中制作一个 ModelForm 。我做了这样的事情:

但它不起作用。选择标签不显示在 html 中。我做错了什么?

更新:

该解决方案可以正常工作,因为我希望它可以正常工作:

0 投票
1 回答
1815 浏览

django - 对 ModelForm() 子项的 save() 中的 self.instance 感到困惑

save() 文档解释说:

ModelForm 的子类可以接受现有模型实例作为关键字参数实例;如果提供了这个, save() 将更新该实例。如果未提供, save() 将创建指定模型的新实例

然而,self.instanceinsave()总是有一个对象。

那么,我如何判断instance是现有的还是新创建的呢?