好的,所以我有一个这样的模型:
class Airplane(models.Model):
tail = models.ForeignKey(Tails)
wheel = models.CharField(max_length=500,blank=True)
window = models.CharField(max_length=500,blank=True)
def clean(self):
if self.tail and self.wheel and self.window:
raise ValidationError("Can't have all three, choose tail and one more")
现在,如果我使用 Django admin 添加新的 Airplane 记录。如果我将两个字段都留空并保存,我会得到指向该行的 Django 错误if self.tail and self.wheel
。
DoesNotExist at /admin/MyProject/airplane/add/
Request Method: POST
Request URL: http://44.101.44.172:8001/admin/MyProject/airplane/add/
Django Version: 1.2.5
Exception Type: DoesNotExist
Exception Value:
Exception Location: /usr/lib/python2.7/site-packages/django/db/models/fields/related.py in __get__, line 299
Python Executable: /usr/bin/python
Python Version: 2.7.0
Django 不应该在运行 clean 之前检查是否填写了必填字段吗?无论如何,处理这个问题的最佳方法是什么?