Changes between Version 1 and Version 2 of Ticket #29738


Ignore:
Timestamp:
Sep 6, 2018, 8:45:08 AM (6 years ago)
Author:
Simon Charette
Comment:

A solution could to make django.db.migration.serializer offer a way to register serializers for types and have django.contrib.postgres.App.ready() be in charge of registering the ones to handle psycopg2 range types.

If the registry uses an OrderedDict we should be able to replace that long series of if by iterating over the registry items.

registry = OrderedDict([
    (models.Field, ModelFieldSerializer),
    ...
])

def serializer_factory(value):
    if isinstance(value, Promise):
        value = str(value)
    elif isinstance(value, LazyObject):
        # The unwrapped value is returned as the first item of the arguments
        # tuple.
    value = value.__reduce__()[1][0]

    for type_, serializer_cls in registry.items():
        if isinstance(value, type_):
            return serializer_cls(value)
    raise ValueError(...)

The hasattr and checks could be performed using abcs.

from abc import ABC
class Deconstructable(ABC):
    @classmethod
    def __subclasshook__(cls, C):
        return hasattr(C, 'deconstruct')

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #29738

    • Property Keywords rangefield postgresql psycopg2 migrations removed
  • Ticket #29738 – Description

    v1 v2  
    1 Tried to use {{{DateTimeTZRange(lower=None, upper=None, bounds='[)')}}} as a default for a model field and get the following error when running {{{python manage.py makemigrations}}}:
     1Tried to use DateTimeTZRange(lower=None, upper=None, bounds='[)') as a default for a model field and get the following error when running 'python manage.py makemigrations':
    22
    33{{{
Back to Top