You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级至Django 4.2后GenericIPField不支持CIDR格式的技术求助

Django升级后GenericIPField不支持CIDR格式的解决方案

升级环境对比

  • 旧环境:Python 3.9 + Postgres 13 + Django 4.0.6 + psycopg2
  • 新环境:Python 3.11 + Postgresql 15 + Django 4.2 + psycopg3

问题描述

升级后使用GenericIPField保存CIDR格式(如10.255.96.48/28)时触发错误:

ValueError: '10.255.96.48/28' does not appear to be an IPv4 or IPv6 address

需保留数据库INET字段的CIDR支持,以执行类似的CIDR关联查询:

Subnet.objects.filter().extra(where = [ "cidr::inet >>= inet '"+self.ip_address+"'" ])

解决方案

1. 自定义兼容CIDR的数据库字段

Django 4.2的GenericIPField加强了IP格式校验,不再接受CIDR。可自定义字段替代:

from django.db import models
from psycopg.types.net import Inet

class CIDRField(models.Field):
    def db_type(self, connection):
        return 'inet'
    
    def from_db_value(self, value, expression, connection):
        if value is None:
            return value
        return str(value)
    
    def to_python(self, value):
        if isinstance(value, str):
            return value
        if isinstance(value, Inet):
            return str(value)
        return super().to_python(value)
    
    def get_prep_value(self, value):
        if value is None:
            return None
        return value

在模型中替换原字段:

class Subnet(models.Model):
    cidr = CIDRField()
    # 其他模型字段...

2. 优化CIDR查询(规避extra方法)

extra方法存在SQL注入风险,推荐用Django ORM原生方式替代:

方式一:自定义查询表达式

from django.db.models import Func, Value

class Inet(Func):
    function = 'INET'
    template = "%(function)s(%(expressions)s)"

class CidrContains(Func):
    function = '>>='
    template = "%(expressions)s %(function)s %(other)s"

# 查询示例
Subnet.objects.filter(
    CidrContains(
        Inet(Value('cidr')),
        Inet(Value(self.ip_address))
    )
)

方式二:参数化RawSQL

from django.db.models import RawSQL

Subnet.objects.annotate(
    matches=RawSQL(
        "cidr::inet >>= inet %s",
        (self.ip_address,)
    )
).filter(matches=True)

3. 配置psycopg3的INET支持

确保psycopg3正确加载网络类型扩展,可在项目初始化代码中添加:

import psycopg
psycopg.extensions.register_type(psycopg.types.net.INET)

内容的提问来源于stack exchange,提问作者EviSvil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 01:52:11