Is there a Django 1.7+ replacement for South's add_introspection_rules()? -
back in days of south migrations, if wanted create custom model field extended django field's functionality, tell south use introspection rules of parent class so:
from south.modelsinspector import add_introspection_rules add_introspection_rules([], ["^myapp\.stuff\.fields\.somenewfield"])
now migrations have been moved django, there non-south equivalent of above? equivalent needed anymore, or new migration stuff smart enough figure out on own?
as phillip mentions in comments, deconstruct()
is official way handle custom fields in django migrations.
to go on complete request clarification... appear there couple of examples of code out there written handle both. example, excerpt (to handle on
parameter exclusivebooleanfield
) taken django-exclusivebooleanfield:
from django.db import models, transaction django.db.models import q 6 import string_types six.moves import reduce try: transaction_context = transaction.atomic except attributeerror: transaction_context = transaction.commit_on_success class exclusivebooleanfield(models.booleanfield): """ usage: class mymodel(models.model): the_one = exclusivebooleanfield() class mymodel(models.model): field_1 = foreignkey() field_2 = charfield() the_one = exclusivebooleanfield(on=('field_1', 'field_2')) # `on` bit unique constraint, value of field # exclusive rows same value of on fields """ def __init__(self, on=none, *args, **kwargs): if isinstance(on, string_types): on = (on, ) self._on_fields = on or () super(exclusivebooleanfield, self).__init__(*args, **kwargs) def contribute_to_class(self, cls, name): super(exclusivebooleanfield, self).contribute_to_class(cls, name) models.signals.class_prepared.connect(self._replace_save, sender=cls) def deconstruct(self): """ support django 1.7 migrations, see add_introspection_rules section @ bottom of file south + earlier django versions """ name, path, args, kwargs = super( exclusivebooleanfield, self).deconstruct() if self._on_fields: kwargs['on'] = self._on_fields return name, path, args, kwargs def _replace_save(self, sender, **kwargs): old_save = sender.save field_name = self.name on_fields = self._on_fields def new_save(self, *args, **kwargs): def reducer(left, right): return left & q(**{right: getattr(self, right)}) transaction_context(): if getattr(self, field_name) true: f_args = reduce(reducer, on_fields, q()) u_args = {field_name: false} sender._default_manager.filter(f_args).update(**u_args) old_save(self, *args, **kwargs) new_save.alters_data = true sender.save = new_save try: south.modelsinspector import add_introspection_rules add_introspection_rules( rules=[ ( (exclusivebooleanfield,), [], {"on": ["_on_fields", {"default": tuple()}]}, ) ], patterns=[ 'exclusivebooleanfield\.fields\.exclusivebooleanfield', ] ) except importerror: pass