Django 1.8.3 - 使用相关对象进行模型字段验证

时间:2015-07-12 19:35:57

标签: python django django-admin foreign-keys

对于以下模型集(Foo,Bar),您可以将以下代码段的Bar.clean中的交叉验证规则强加到django 1.7。

相同的代码段在django 1.8.3中引发RelatedObjectDoesNotExist错误。

在django 1.8.3中实现相同结果的新方法和改进方法是什么?

(我已经包含了admin.py代码,仅用于说明如何使用这些模型。)

models.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.')

admin.py

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)

1 个答案:

答案 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.')