- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在弄清楚如何在提交表单时自动为 ForeignKey 字段创建模型实例时遇到了很多麻烦。这是一个说明问题的简单玩具网站:
我有两个模型,模型 1 和模型 2。 Model2 包含 Model1 的 ForeignKey。我希望用户能够通过专门选择 Model1 的实例存储在 ForeignKey 中,或者将该值留空并让 Model1 的实例自动生成来创建 Model2 的实例。
我觉得代码应该是这样的。我的 models.py 代码非常简单:
# models.py
from django.db import models
from django.core.validators import MinValueValidator
class Model1(models.Model):
# Note this field cannot be negative
my_field1 = models.IntegerField(validators=[MinValueValidator(0)])
class Model2(models.Model):
# blank = True will make key_to_model1 not required on the form,
# but since null = False, I will still require the ForeignKey
# to be set in the database.
related_model1 = models.ForeignKey(Model1, blank=True)
# Note this field cannot be negative
my_field2 = models.IntegerField(validators=[MinValueValidator(0)])
forms.py 有点复杂,但是发生的事情非常简单。如果 Model2Form 没有收到 Model1 的实例,它会尝试在 clean 方法中自动创建一个实例,对其进行验证,如果有效,则会将其保存。如果无效,则会引发异常。
#forms.py
from django import forms
from django.forms.models import model_to_dict
from .models import Model1, Model2
# A ModelForm used for validation purposes only.
class Model1Form(forms.ModelForm):
class Meta:
model = Model1
class Model2Form(forms.ModelForm):
class Meta:
model = Model2
def clean(self):
cleaned_data = super(Model2Form, self).clean()
if not cleaned_data.get('related_model1', None):
# Don't instantiate field2 if it doesn't exist.
val = cleaned_data.get('my_field2', None)
if not val:
raise forms.ValidationError("My field must exist")
# Generate a new instance of Model1 based on Model2's data
new_model1 = Model1(my_field1=val)
# validate the Model1 instance with a form form
validation_form_data = model_to_dict(new_model1)
validation_form = Model1Form(validation_form_data)
if not validation_form.is_valid():
raise forms.ValidationError("Could not create a proper instance of Model1.")
# set the model1 instance to the related model and save it to the database.
new_model1.save()
cleaned_data['related_model1'] = new_model1
return cleaned_data
但是,这种方法行不通。如果我在我的表单中输入有效数据,它就可以正常工作。但是,如果我没有为 ForeignKey 输入任何内容并为整数输入一个负值,我会得到一个 ValueError。
Traceback: File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) File "/Library/Python/2.7/site-packages/django/views/generic/base.py" in view 48. return self.dispatch(request, *args, **kwargs) File "/Library/Python/2.7/site-packages/django/views/generic/base.py" in dispatch 69. return handler(request, *args, **kwargs) File "/Library/Python/2.7/site-packages/django/views/generic/edit.py" in post 172. return super(BaseCreateView, self).post(request, *args, **kwargs) File "/Library/Python/2.7/site-packages/django/views/generic/edit.py" in post 137. if form.is_valid(): File "/Library/Python/2.7/site-packages/django/forms/forms.py" in is_valid 124. return self.is_bound and not bool(self.errors) File "/Library/Python/2.7/site-packages/django/forms/forms.py" in _get_errors 115. self.full_clean() File "/Library/Python/2.7/site-packages/django/forms/forms.py" in full_clean 272. self._post_clean() File "/Library/Python/2.7/site-packages/django/forms/models.py" in _post_clean 309. self.instance = construct_instance(self, self.instance, opts.fields, opts.exclude) File "/Library/Python/2.7/site-packages/django/forms/models.py" in construct_instance 51. f.save_form_data(instance, cleaned_data[f.name]) File "/Library/Python/2.7/site-packages/django/db/models/fields/init.py" in save_form_data 454. setattr(instance, self.name, data) File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py" in set 362. (instance._meta.object_name, self.field.name))
Exception Type: ValueError at /add/ Exception Value: Cannot assign None: "Model2.related_model1" does not allow null values.
所以,发生的事情是 Django 捕获了我的 ValidationError 并仍在创建 Model2 的实例,即使验证失败也是如此。
我可以通过重写 _post_clean 方法来解决这个问题,以便在出现错误时不创建 Model2 的实例。但是,这个解决方案很丑陋。特别是,_post_clean 的行为通常非常有用——在更复杂的项目中,出于其他原因我需要运行 _post_clean。
我也可以允许 ForeignKey 为 null 但在实践中永远不会将其设置为 null。但是,再一次,这似乎是个坏主意。
我什至可以设置一个虚拟 Model1,每当对尝试的新 Model1 的验证失败时我都会使用它,但这看起来也很老套。
一般来说,我可以想出很多 hack 来解决这个问题,但我不知道如何以一种相当干净的 pythonic 方式解决这个问题。
最佳答案
基于 karthikr 在评论中的讨论,我找到了一个我认为可以接受的解决方案。我绝对对其他选择持开放态度。
想法是在 View 中使用逻辑在两种形式之间进行选择以进行验证:一种形式是标准模型形式,一种是没有 ForeignKey 字段的模型形式。
所以,我的 models.py 是相同的。
我的 forms.py 有两种 Model2 形式……一种非常简单,另一种没有 ForeignKey 字段,但具有新的逻辑,可以为 ForeignKey 动态生成 Model1 的新实例。新表单的简洁逻辑就是我用来放入 Model2Form 的简洁逻辑:
#forms.py
from django import forms
from django.forms.models import model_to_dict
from .models import Model1, Model2
# A ModelForm used for validation purposes only.
class Model1Form(forms.ModelForm):
class Meta:
model = Model1
class Model2Form(forms.ModelForm):
class Meta:
model = Model2
# This inherits from Model2Form so that any additional logic that I put in Model2Form
# will apply to it.
class Model2FormPrime(Model2Form):
class Meta:
model = Model2
exclude = ('related_model1',)
def clean(self):
cleaned_data = super(Model2Form, self).clean()
if cleaned_data.get('related_model1', None):
raise Exception('Huh? This should not happen...')
# Don't instantiate field2 if it doesn't exist.
val = cleaned_data.get('my_field2', None)
if not val:
raise forms.ValidationError("My field must exist")
# Generate a new instance of Model1 based on Model2's data
new_model1 = Model1(my_field1=val)
# validate the Model1 instance with a form form
validation_form_data = model_to_dict(new_model1)
validation_form = Model1Form(validation_form_data)
if not validation_form.is_valid():
raise forms.ValidationError("Could not create a proper instance of Model1.")
# set the Model1 instance to the related model and save it to the database.
cleaned_data['related_model1'] = new_model1
return cleaned_data
def save(self, commit=True):
# Best to wait til save is called to save the instance of Model1
# so that instances aren't created when the Model2Form is invalid
self.cleaned_data['related_model1'].save()
# Need to handle saving this way because otherwise related_model1 is excluded
# from the save due to Meta.excludes
instance = super(Model2FormPrime, self).save(False)
instance.related_model1 = self.cleaned_data['related_model1']
instance.save()
return instance
然后我的 View 逻辑使用两种形式之一进行验证,具体取决于发布数据。如果它使用 Model2FormPrime 并且验证失败,它会将数据和错误移动到常规 Model2Form 以向用户显示:
# Create your views here.
from django.views.generic.edit import CreateView
from django.http import HttpResponseRedirect
from .forms import Model2Form, Model2FormPrime
class Model2CreateView(CreateView):
form_class = Model2Form
template_name = 'form_template.html'
success_url = '/add/'
def post(self, request, *args, **kwargs):
if request.POST.get('related_model', None):
# Complete data can just be sent to the standard CreateView form
return super(Model2CreateView, self).post(request, *args, **kwargs)
else:
# super does this, and I won't be calling super.
self.object = None
# use Model2FormPrime to validate the post data without the related model.
validation_form = Model2FormPrime(request.POST)
if validation_form.is_valid():
return self.form_valid(validation_form)
else:
# Create a normal instance of Model2Form to be displayed to the user
# Insantiate it with post data and validation_form's errors
form = Model2Form(request.POST)
form._errors = validation_form._errors
return self.form_invalid(form)
此解决方案有效,而且非常灵活。我可以向我的模型和基本 Model2Form 添加逻辑,而不必太担心破坏它或违反 DRY。
不过,它有点难看,因为它要求我使用两种形式来完成一种工作,即在形式之间传递错误。因此,如果有人可以提出任何建议,我绝对愿意接受其他解决方案。
关于python - Django:ValueError:尝试在干净的环境中创建时无法在 ForeignField 上分配任何内容失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15817437/
我有一个应用程序,它会抛出 GKSession 并在各种条件下(连接超时、 session 失败等)创建一个新的 GKSession。不过,我遇到了内存泄漏问题,并且有时会在重新连接几次循环后崩溃。
比如我在宿主代码中有一个浮点指针 float *p 是否可以确定他指向的内存类型(设备/主机)? 最佳答案 在 UVA system 中, 运行时 API 函数 cudaPointerGetAttri
我已将项目转换为 .Net 4.0 并且以下代码不起作用: typeof(RuntimeTypeHandle).GetMethod("Allocate", BindingFlags.Instance
当我声明 char ch = 'ab' 时,ch 只包含 'b',为什么它不存储 'a'? #include int main() { char ch = 'ab'; printf("%c"
我对 Disk Sector 和 Block 有疑问。扇区是一个单位,通常为 512 字节或 1k、2k、4k 等取决于硬件。文件系统 block 大小是一组扇区大小。 假设我正在存储一个 5KB 的
假设我有 8 个人和5000 个苹果。 我想将所有苹果分发给所有 8 个人,这样我就没有苹果了。 但每个人都应该得到不同数量 将它们全部分发出去的最佳方式是什么? 我是这样开始的: let peopl
我正在构建的网站顶部有一个搜索栏。与 Trello 或 Gmail 类似,我希望当用户按下“/”键时,他们的焦点就会转到该搜索框。 我的 JavaScript 看起来像这样: document.onk
我有一小段代码: if (PZ_APP.dom.isAnyDomElement($textInputs)){ $textInputs.on("focus", function(){
我观察到以下行为。 接受了两个属性变量。 @property (nonatomic, retain) NSString *stringOne; @property (nonatomic, assign
我正在解决这样的问题 - 实现一个计算由以下内容组成的表达式的函数以下操作数:“(”、“)”、“+”、“-”、“*”、“/”。中的每个数字表达式可能很大(与由字符串表示的一样大)1000 位)。 “/
我有一组主机和一组任务。 每个主机都有 cpu、mem 和任务容量,每个任务都有 cpu、mem 要求。 每个主机都属于一个延迟类别,并且可以与具有特定延迟值的其他主机通信。 每个任务可能需要以等于或
该程序的作用:从文件中读取一个包含 nrRows 行和 nrColomns 列的矩阵(二维数组)。矩阵的所有元素都是 [0,100) 之间的整数。程序必须重新排列矩阵内的所有元素,使每个元素等于其所在
世界!我有个问题。今天我尝试创建一个代码,它可以找到加泰罗尼亚语号码。但是在我的程序中可以是长数字。我找到了分子和分母。但我不能分割长数字!此外,只有标准库必须在此程序中使用。请帮帮我。这是我的代码
我确定我遗漏了一些明显的东西,但我想在 Objective C 中创建一个 NSInteger 指针的实例。 -(NSInteger*) getIntegerPointer{ NSInteger
这个问题在这里已经有了答案: Difference between self.ivar and ivar? (4 个答案) 关闭 9 年前。
我如何将 v[i] 分配给一系列整数(v 的类型是 vector )而无需最初填充 最佳答案 你的意思是将 std::vector 初始化为一系列整数? int i[] = {1, 2, 3, 4,
我想寻求分配方面的帮助....我把这个作业带到了学校......我必须编写程序来加载一个 G 矩阵和第二个 G 矩阵,并搜索第二个 G 矩阵以获取存在数第一个 G 矩阵的......但是,当我尝试运行
我必须管理资源。它基本上是一个唯一的编号,用于标识交换机中的第 2 层连接。可以有 16k 个这样的连接,因此每次用户希望配置连接时,他/她都需要分配一个唯一索引。同样,当用户希望删除连接时,资源(号
是否有任何通用的命名约定来区分已分配和未分配的字符串?我正在寻找的是希望类似于 us/s 来自 Making Wrong Code Look Wrong ,但我宁愿使用常见的东西也不愿自己动手。 最佳
我需要读取一个 .txt 文件并将文件中的每个单词分配到一个结构中,该结构从结构 vector 指向。我将在下面更好地解释。 感谢您的帮助。 我的程序只分配文件的第一个字... 我知道问题出在函数 i
我是一名优秀的程序员,十分优秀!