简体   繁体   English

如何验证 objects.create() 方法的选择?

[英]How to validate CHOICES for objects.create() method?

I have the following model:我有以下模型:

from django.db import models


class Artist(models.Model):

    TYPE_CHOICES = (
        ('Person', 'Person'),
        ('Group', 'Group'),
        ('Other', 'Other'),)

    name = models.CharField(max_length=100)
    type = models.CharField(max_length=20, choices=TYPE_CHOICES)

The problem is that if I create an object like this: Artist.objects.create(...) the type validation doesn't work.问题是,如果我创建这样的对象: Artist.objects.create(...)类型验证不起作用。 How can I activate the validation for this?我怎样才能为此激活验证?

You can make an (abstract) model that first performs validations before saving the object with:您可以创建一个(抽象)模型,在保存对象之前首先执行验证:

class ValidatedModel(models.Model):

    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        self.clean_fields()      # validate individual fields
        self.clean()             # validate constraints between fields
        self.validate_unique()   # validate uniqness of fields
        return super(ValidatedModel, self).save(*args, **kwargs)

and then for example use this in models like:然后例如在以下模型中使用它:

class Artist(ValidatedModel):

    TYPE_CHOICES = (
        ('Person', 'Person'),
        ('Group', 'Group'),
        ('Other', 'Other'),)

    name = models.CharField(max_length=100)
    type = models.CharField(max_length=20, choices=TYPE_CHOICES)

Note that the above will validate model object in case you call the .save() method (or some other function does that), but some methods circumvent calling the .save() method like Model.objects.bulk_create(..) , etc.请注意,如果您调用.save()方法(或其他一些函数这样做),上面将验证模型对象,但有些方法会绕过调用.save()方法,如Model.objects.bulk_create(..)等.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ManyToMany字段作为.objects.create()函数的参数 - ManyToMany field as an argument to .objects.create() function "Django Postgres objects.create ValidationError" - Django Postgres objects.create ValidationError django orm 中objects.create() 和object.save() 的区别 - difference between objects.create() and object.save() in django orm Django模型管理器objects.create文档在哪里? - Django model manager objects.create where is the documentation? 在第二个object.create之后Django回滚不起作用 - Django rollback not working after second objects.create fails to create a record Django get_user_model().objects.create() 密码没有得到散列 - Django get_user_model().objects.create() password is not getting hashed orm [&#39;model&#39;]。objects.create(attr = some_value)导致IntegrityError - orm['model'].objects.create(attr=some_value) causes IntegrityError 在Django测试中,为什么我需要使用 <Model> .objects.get()而不是返回的内容 <Model> .objects.create()? - In Django tests, why do I need to use <Model>.objects.get() instead of what was returned by <Model>.objects.create()? 如何根据预设的选择列表验证Django ModelForm中的选择数组 - How to validate array of choices in Django ModelForm against preset list of choices 如何在序列化程序创建方法中创建多个对象? - How to create multiple objects in serializers create method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM