对于以下模型集(Foo,Bar),您可以将以下代码段的Bar.clean中的交叉验证规则强加到django 1.7。
相同的代码段在django 1.8.3中引发RelatedObjectDoesNotExist
错误。
在django 1.8.3中实现相同结果的新方法和改进方法是什么?
(我已经包含了admin.py代码,仅用于说明如何使用这些模型。)
from django.db import models
from django.core.exceptions import ValidationError
class Foo(models.Model):
name = models.CharField("Name", blank=True, max_length=300)
class Bar(models.Model):
name = models.CharField("Name", blank=True, max_length=300)
foo = models.ForeignKey('Foo', verbose_name='Foo')
def clean(self):
if self.name + self.foo.name != 'FooBar':
raise ValidationError('Concatenation should be FooBar.')
from django.contrib import admin
import models
class BarInline(admin.TabularInline):
model = models.Bar
class FooAdmin(admin.ModelAdmin):
model = models.Foo
inlines = [BarInline,]
site = admin.site
site.register(models.Foo,FooAdmin)
答案 0 :(得分:1)
我在代码中添加了一个简单的输出修改
def clean(self):
print(self.__dict__)
if self.name + self.foo.name != 'FooBar':
raise ValidationError('Concatenation should be FooBar.')
简单的print语句将在执行主代码之前打印出Bar对象。
现在我用Django 1.8.x测试了代码,我得到了例外,因为你提到这是结果:
{'_state': <django.db.models.base.ModelState object at 0x7ff55cd30710>, 'id': None, 'foo_id': None, 'name': 'Bar 1'}
现在我已经使用Django 1.7.x再次测试它并且它工作正常,输出结果是:
{'_foo_cache': <Foo: Foo object>, 'name': 'Bar 1', 'id': None, 'foo_id': None, '_state': <django.db.models.base.ModelState object at 0x7f731151c9e8>}
正如您在foo_id
中的两种情况中都注意到None
但是在Django 1.8中删除的_foo_cache
内容的神奇之处
我建议您使用的替代方法是将验证移至表单
进行了以下更改: admin.py
class BarInline(admin.TabularInline):
model = Bar
form = BarForm
forms.py
class BarForm(forms.models.ModelForm):
class Meta:
model = Bar
fields = ('name',)
def clean(self):
data = self.cleaned_data
if not data['name'] + data['foo'].name == "foobar":
raise ValidationError('Concatenation should be FooBar.')