Skip to content

Duplicated GenericIPAddressField validation errors with ModelSerializer and protocol #9645

Open
@HansAarneLiblik

Description

@HansAarneLiblik

Hi!

I'm using GenericIPAddressField with explicit protocol as IPv4 (Also reproduces with IPv6) and ModelSerializer. If the input is not an valid IpAddress, DRF generates duplicate(ish) errors.

Repro

from django.db import models
from rest_framework.serializers import ModelSerializer


class A(models.Model):
    address = models.GenericIPAddressField(protocol="IPv4")  # Also reproduces with `IPv6`

    class Meta:
        app_label = "main"

class ASerializer(ModelSerializer[A]):
    class Meta:
        model = A
        fields = "__all__"


ser = ASerializer(data={'address': 'not an ip address'})
ser.is_valid(raise_exception=True)

Result

rest_framework.exceptions.ValidationError: {'address': [
    ErrorDetail(string='Enter a valid IPv4 address.', code='invalid'), 
    ErrorDetail(string='Enter a valid IPv4 or IPv6 address.', code='invalid')
]}

The second error message is incorrect

Thanks!

  • Raised initially as discussion - Can't, it gets deleted automatically - https://groups.google.com/g/django-rest-framework/c/eNNq1UYeww4
  • This is not a feature request suitable for implementation outside this project. Please elaborate what it is:
    • other type of bug fix
  • I have reduced the issue to the simplest possible case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions