class Meta: # 一般做收藏的话。最好能返回收藏数据的id。便于删除 model = UserFav # 设置字段为联合唯一索引 validators = [ UniqueTogetherValidator( queryset=UserFav.objects.all(), fields=('user', 'goods'), message="已经收藏", ) ] fields = ("user", "goods", "id")
def validate(self, data): # Validate uniqueness of (site, slug) since we omitted the automatically-created validator from Meta. # TODO: Remove if/when slug is globally unique. This would be a breaking change. if data.get("slug", None): validator = UniqueTogetherValidator( queryset=RackGroup.objects.all(), fields=("site", "slug")) validator(data, self) # Enforce model validation super().validate(data) return data
def validate(self, data): # Validate uniqueness of (group, facility_id) since we omitted the automatically-created validator from Meta. if data.get("facility_id", None): validator = UniqueTogetherValidator(queryset=Rack.objects.all(), fields=("group", "facility_id")) validator(data, self) # Enforce model validation super().validate(data) return data
def validate(self, data): # Validate uniqueness of vid and name if a group has been assigned. if data.get("group", None): for field in ["vid", "name"]: validator = UniqueTogetherValidator(queryset=VLAN.objects.all(), fields=("group", field)) validator(data, self) # Enforce model validation super().validate(data) return data
class Meta: # model = Booking # fields = '__all__' """ https://www.django-rest-framework.org/api-guide/validators/ infos about unique validators in the link. """ validators = [ UniqueTogetherValidator( queryset=Booking.objects.all(), fields=['psychologist', 'date', 'time'] )]
class Meta: model = Product fields = ('article', 'name', 'group', 'price', 'quantity', ) read_only_fields = ('id', ) validators = [ UniqueTogetherValidator( queryset=Product.objects.all(), fields=['group', 'name'], message="Название товара в группе должно быть уникальным" ) ]
def validate(self, data): # Validate uniqueness of name and slug if a site has been assigned. if data.get("site", None): for field in ["name", "slug"]: validator = UniqueTogetherValidator(queryset=VLANGroup.objects.all(), fields=("site", field)) validator(data, self) # Enforce model validation super().validate(data) return data
class Meta: model = Patient fields = [ 'id', 'firstName', 'lastName', 'phone', 'details', 'createdBy', 'updatedBy', 'createdAt', 'updatedAt' ] validators = [ UniqueTogetherValidator( queryset=Patient.objects.all(), fields=['firstName', 'lastName'], message="Cannot add patient. Reason: Patient already exists!") ]
class Meta: model = Doctor fields = [ 'id', 'firstName', 'lastName', 'cabinet', 'phone', 'createdBy', 'updatedBy', 'createdAt', 'updatedAt' ] validators = [ UniqueTogetherValidator( queryset=Doctor.objects.all(), fields=['firstName', 'lastName'], message="Cannot add doctor. Reason: Doctor already exists!") ]
class Meta: validators = [ UniqueTogetherValidator( queryset=UserFav.objects.all(), fields=('user', 'goods'), message='已经收藏', ) ] model = UserFav fields = ('user', 'goods', 'id' ) # 收藏的时候需要返回商品的id,因为取消收藏的时候必须知道商品的id是多少
class Meta: model = CollectionAddon fields = ('addon', 'notes', 'collection') validators = [ UniqueTogetherValidator( queryset=CollectionAddon.objects.all(), message=_('This add-on already belongs to the collection'), fields=('addon', 'collection'), ), ] writeable_fields = ('notes', ) read_only_fields = tuple(set(fields) - set(writeable_fields))
class Meta: #validate实现唯一联合,一个商品只能收藏一次 validators = [ UniqueTogetherValidator( queryset=UserFav.objects.all(), fields=('user', 'goods'), #message的信息可以自定义 message="已经收藏") ] model = UserFav #收藏的时候需要返回商品的id,因为取消收藏的时候必须知道商品的id是多少 fields = ("user", "goods", 'id')
class Meta: #反正Meta validators = [ UniqueTogetherValidator( #填写的Molde要是model = UserFav queryset=UserFav.objects.all(), #某个用户对某个商品只能收藏一次,但是多个用户对该商品是可以收藏的 fields=('user', 'goods'), message="一个用户对某个商品只能收藏一次") ] model = UserFav fields = ("user", "goods", "id")
class Meta: model = Game fields = [ 'id', 'team_a', 'team_b', 'game_time', 'sport', 'region', 'league' ] validators = [ UniqueTogetherValidator(queryset=Game.objects.all(), fields=[ 'team_a', 'team_b', 'game_time', 'sport', 'region', 'league' ]) ]
class Meta: model = Department validators = [ UniqueTogetherValidator(queryset=model.objects.all(), fields=('departmentName', 'hod')) ] fields = ('__all__') extra_kwargs = { 'departmentName': { 'validators': [UniqueValidator(queryset=model.objects.all())], } }
class Meta: model = Rack fields = [ 'id', 'url', 'display', 'name', 'facility_id', 'display_name', 'site', 'location', 'tenant', 'status', 'role', 'serial', 'asset_tag', 'type', 'width', 'u_height', 'desc_units', 'outer_width', 'outer_depth', 'outer_unit', 'comments', 'tags', 'custom_fields', 'created', 'last_updated', 'device_count', 'powerfeed_count', ] # Omit the UniqueTogetherValidator that would be automatically added to validate (location, facility_id). This # prevents facility_id from being interpreted as a required field. validators = [ UniqueTogetherValidator(queryset=Rack.objects.all(), fields=('location', 'name')) ]
def validate(self, data): # Validate uniqueness of (rack, position, face) since we omitted the automatically-created validator from Meta. if data.get("rack") and data.get("position") and data.get("face"): validator = UniqueTogetherValidator(queryset=Device.objects.all(), fields=("rack", "position", "face")) validator(data, self) # Enforce model validation super().validate(data) return data
class Meta: # 設定validate使一個coupons只能收藏一次 validators = [ #只能一個 UniqueTogetherValidator( queryset=UserFav.objects.all(), fields=('user', 'coupons'), # message的信息可以自定义 message="已經收藏") ] model = UserFav # 必填,因為取消收藏時要知道ID,所以ID也是必填 fields = ("user", "coupons", 'id')
def validate(self, data): # Validate uniqueness of (site, facility_id) since we omitted the automatically-created validator from Meta. if data.get('facility_id', None): validator = UniqueTogetherValidator(queryset=Rack.objects.all(), fields=('site', 'facility_id')) validator.set_context(self) validator(data) # Enforce model validation super(WritableRackSerializer, self).validate(data) return data
def validate(self, data): # Validate uniqueness of (virtual_chassis, position) validator = UniqueTogetherValidator( queryset=VCMembership.objects.all(), fields=('virtual_chassis', 'position')) validator.set_context(self) validator(data) # Enforce model validation super(WritableVCMembershipSerializer, self).validate(data) return data
class Meta: model = CallDetail fields = ('url', 'id', 'type', 'timestamp', 'source', 'destination', 'call_id') validators = [ UniqueTogetherValidator( queryset=CallDetail.objects.all(), fields=('type', 'call_id'), message= 'A detail record with this type and call id has already been sent. Delete it before resend it.', ) ]
class Meta: model = User fields = ( 'username', 'email', 'sex', 'country', 'password', ) validators = [ UniqueTogetherValidator(queryset=User.objects.all(), fields=['email', 'username']) ]
class Meta: model = Favorite fields = ( 'user', 'todo_list', ) validators = [ UniqueTogetherValidator(queryset=model.objects.all(), fields=( 'user', 'todo_list', )) ]
class Meta: model = UserFav # 使用validate方式实现唯一联合 validators = [ UniqueTogetherValidator( queryset=UserFav.objects.all(), fields=('user', 'products'), message="Favorited" ) ] fields = ("user", "products", "id")
def validate(self, data): # Validate uniqueness of name and slug if a site has been assigned. if data.get('site', None): for field in ['name', 'slug']: validator = UniqueTogetherValidator(queryset=VLANGroup.objects.all(), fields=('site', field)) validator.set_context(self) validator(data) # Enforce model validation super(VLANGroupSerializer, self).validate(data) return data
class Meta: model = User fields = ( 'username', 'first_name', 'last_name', 'email', 'password', ) validators = [ UniqueTogetherValidator(queryset=User.objects.all(), fields=['username', 'email']) ]
class Meta: model = UserLink fields = '__all__' validators = [ UniqueTogetherValidator(queryset=UserLink.objects.all(), fields=( 'userid', 'userid_to', 'level', ), message="登录名重复!"), ]
class Meta: model = UserFav # 收藏唯一性验证 validators = [ UniqueTogetherValidator( queryset=UserFav.objects.all(), fields=['user', 'goods'], message="该商品已经收藏", ) ] fields = ("user", "goods", "id")
def validate(self, data): # Validate uniqueness of vid and name if a group has been assigned. if data.get('group', None): for field in ['vid', 'name']: validator = UniqueTogetherValidator(queryset=VLAN.objects.all(), fields=('group', field)) validator.set_context(self) validator(data) # Enforce model validation super(VLANSerializer, self).validate(data) return data
class Meta: model = Rack fields = ['id', 'rack_number', 'datacenter', 'u1', 'u2', 'u3', 'u4', 'u5', 'u6', 'u7', 'u8', 'u9', 'u10', 'u11', 'u12', 'u13', 'u14', 'u15', 'u16', 'u17', 'u18', 'u19', 'u20', 'u21', 'u22', 'u23', 'u24', 'u25', 'u26', 'u27', 'u28', 'u29', 'u30', 'u31', 'u32', 'u33', 'u34', 'u35', 'u36', 'u37', 'u38', 'u39', 'u40', 'u41', 'u42'] validators = [ UniqueTogetherValidator( queryset=Rack.objects.all(), fields=['datacenter', 'rack_number'] ) ]