mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 01:57:00 +03:00
added RegexField
This commit is contained in:
parent
44e9749e36
commit
86484668f6
|
@ -141,6 +141,16 @@ A text representation, validates the text to be a valid e-mail address.
|
|||
|
||||
Corresponds to `django.db.models.fields.EmailField`
|
||||
|
||||
## RegexField
|
||||
|
||||
A text representation, that validates the given value matches against a certain regular expression.
|
||||
|
||||
Uses Django's `django.core.validators.RegexValidator` for validation.
|
||||
|
||||
Corresponds to `django.forms.fields.RegexField`
|
||||
|
||||
**Signature:** `RegexField(regex, max_length=None, min_length=None)`
|
||||
|
||||
## DateField
|
||||
|
||||
A date representation.
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
## Master
|
||||
|
||||
* Support for `read_only_fields` on `ModelSerializer` classes.
|
||||
* Added `RegexField`.
|
||||
|
||||
## 2.1.2
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import copy
|
||||
import datetime
|
||||
import inspect
|
||||
import re
|
||||
import warnings
|
||||
|
||||
from django.core import validators
|
||||
|
@ -768,6 +769,34 @@ class EmailField(CharField):
|
|||
return result
|
||||
|
||||
|
||||
class RegexField(CharField):
|
||||
type_name = 'RegexField'
|
||||
|
||||
def __init__(self, regex, max_length=None, min_length=None, *args, **kwargs):
|
||||
super(RegexField, self).__init__(max_length, min_length, *args, **kwargs)
|
||||
self.regex = regex
|
||||
|
||||
def _get_regex(self):
|
||||
return self._regex
|
||||
|
||||
def _set_regex(self, regex):
|
||||
if isinstance(regex, basestring):
|
||||
regex = re.compile(regex)
|
||||
self._regex = regex
|
||||
if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
|
||||
self.validators.remove(self._regex_validator)
|
||||
self._regex_validator = validators.RegexValidator(regex=regex)
|
||||
self.validators.append(self._regex_validator)
|
||||
|
||||
regex = property(_get_regex, _set_regex)
|
||||
|
||||
def __deepcopy__(self, memo):
|
||||
result = copy.copy(self)
|
||||
memo[id(self)] = result
|
||||
result.validators = self.validators[:]
|
||||
return result
|
||||
|
||||
|
||||
class DateField(WritableField):
|
||||
type_name = 'DateField'
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user