Django 1.1.1,需要依赖其他字段的自定义验证
我的 Django 应用程序中有 3 个模型,每个模型都有一个“主机名”字段。由于多种原因,这些在不同的模型中进行跟踪:
class device(models.Model):
...
hostname = models.CharField(max_length=45, unique=True, help_text="The hostname for this device")
...
class netdevice(models.Model):
...
hostname = models.CharField(max_length=45, unique=True, help_text="Name Associated with Device", verbose_name="Hostname")
...
class vipdevice(models.Model):
...
hostname = models.CharField(max_length=45, unique=True, help_text="Name associated with this Virtual IP", verbose_name="name")
...
如何设置验证以确保主机名字段不会在 3 个模型中的任何一个中重复?
我看过 http://docs.djangoproject.com/ en/dev/ref/validators/#ref-validators,但我不确定这是否是正确的路径。特别是在函数内从其他类创建对象等。
I have 3 models in a Django app, each one has a "hostname" field. For several reasons, these are tracked in different models:
class device(models.Model):
...
hostname = models.CharField(max_length=45, unique=True, help_text="The hostname for this device")
...
class netdevice(models.Model):
...
hostname = models.CharField(max_length=45, unique=True, help_text="Name Associated with Device", verbose_name="Hostname")
...
class vipdevice(models.Model):
...
hostname = models.CharField(max_length=45, unique=True, help_text="Name associated with this Virtual IP", verbose_name="name")
...
How can I set up for validation to make sure hostname fields aren't duplicated across any of the 3 models?
I've looked at http://docs.djangoproject.com/en/dev/ref/validators/#ref-validators, but I'm not sure if that's the right path or not. Especially with creating objects from other classes inside a function, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用模型继承。就像这样:
不要将 BaseDevice 定义为
抽象
模型。You could use Model Inheritance. Like so:
Don't define BaseDevice as
abstract
Model.