Skip to content

Commit c141c93

Browse files
Handle pyscopg3 specific adapters
1 parent 4ddb5d4 commit c141c93

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

bitfield/types.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from django.core.exceptions import ImproperlyConfigured
2+
3+
14
def cmp(a, b):
25
return (a > b) - (a < b)
36

@@ -257,3 +260,22 @@ def get_label(self, flag):
257260
Database.extensions.register_adapter(BitHandler, lambda x: Database.extensions.AsIs(int(x)))
258261
except ImproperlyConfigured:
259262
pass
263+
264+
# psycopg3 adapter registration: register a dumper that
265+
# encodes Bit/BitHandler as BIGINT so parameters are typed correctly.
266+
try:
267+
from psycopg import adapters
268+
from psycopg.adapt import Dumper
269+
except Exception:
270+
pass
271+
else:
272+
273+
class _BitLikeDumper(Dumper):
274+
# OID 20 is int8 (BIGINT) in PostgreSQL, matching Django's BigIntegerField
275+
oid = 20
276+
277+
def dump(self, obj):
278+
return str(int(obj)).encode()
279+
280+
adapters.register_dumper(Bit, _BitLikeDumper)
281+
adapters.register_dumper(BitHandler, _BitLikeDumper)

0 commit comments

Comments
 (0)