Mandate integer type in django-rest-framework validator for integerfield


django-rest-framework validator accepts string quoted integers as valid data for integer field. Is there a way to mandate data type so that string quoted integers won't be accepted ?


Serializer keeps raw data in self.initial_data from where custom validation methods can retrieve it. So the following works

<pre class="lang-py prettyprint-override">class Test(Serializer): true_int = IntegerField() def validate_true_int(self, value): value = self.initial_data.get('true_int') if isinstance(value, int): return value raise ValidationError("Not an int") data = {'true_int': '10'} t = Test(data=data) t.is_valid() # false data = {'true_int': 10} t = Test(data=data) t.is_valid() # true

<strike>You can add a custom validator to the field. Custom Validator</strike>

You can create a custom serializer field that errors before conversion to an integer.

<pre class="lang-py prettyprint-override">class TrueIntegerField(serializers.IntegerField): def to_internal_value(self, data): if isinstance(data, six.text_type): raise serializers.ValidationError("Value should be an integer") return super().to_internal_value(data)

You can write field validation

def validate_field_name(self): validating_data = self.context.get('request').data.get('field_name') if isinstance(validating_data, int): return validating_data raise ValidationError('Integer value required')



  • Mandate integer type in django-rest-framework validator for integerfield
  • How to add string numbers in pandas [duplicate]
  • Filtering lists
  • Is steady_clock monotonic across threads?
  • Is multiple .Where() statements in LINQ a performance issue?
  • Warning: Attempt to present on whose view is not in the window hierarchy -Swift
  • Force jaxb unmarshaller to ignore html Tags
  • I am trying to bind a text area to an angularjs variable and have it reflect changes I make to the t
  • Why UIWebView work so slowly when loadHTMLString with UIWebView?
  • Java, How to refresh JTable in one frame from another frame
  • Replace Windows command FTP -s:E:\\FtpScript.txt with SFTP? [duplicate]
  • Linq query with a Conditional WHERE Clause
  • Use animate() with series of levelplots in R raster
  • Remove Textmate Git Bundle
  • Rails Template Error with Heroku
  • Set selected using jQuery
  • How to check if the current repository is the top level git repo
  • Create One-To-One relationship between table and view in EF4?
  • Bazel failed to include a external static library .a
  • Unable to connect to Azure MySQL Database through Azure Function - C#
  • Adding Dynamic Row and Data on Checkbox Click
  • Scheme procedure to compute the nth repeated application of a function?
  • Delete a ListItem by clicking the ImageView within the ListItem
  • Issue with Terrain Collision using Three.js
  • Find all parks for a given zipcode with google maps
  • How to output data of primefaces DataGrid component by columns instead of by rows (transpose data)?
  • How to use Typescript with libraries like Ampersand.js that parse configs to build prototypes
  • Magento-Change Attribute of All Products
  • Django, uWSGI & nginx: Process dies for “no reason”
  • Floating parent div grows to hypothetical width of floating child div
  • Stacked bar chart with continuous time-axis as x-axis
  • How to warp text around image in iOS?