def add_fields(self, form, index): from restorm.fields import ToOneField with patch('django.db.models.ForeignKey', ToOneField): with patch('django.forms.models.ModelChoiceField', ResourceChoiceField): result = super(BaseRestFormSet, self).add_fields(form, index) return result
def inlinerestformset_factory(*args, **kwargs): from django.forms.models import ModelForm from django.forms.models import inlineformset_factory as _dj_inlineformset_factory if kwargs.get('form') == ModelForm: kwargs['forms'] = RestForm with patch('django.forms.models.ModelForm', RestForm): with patch('django.forms.models.BaseInlineFormSet', BaseRestFormSet): with patch('django.forms.models._get_foreign_key', _get_foreign_key): return _dj_inlineformset_factory(*args, **kwargs)
def render_change_form(self, *args, **kwargs): class ContentType: pk = None content_type = ContentType() def get_content_type_for_model(model): return content_type with patch('django.contrib.admin.options.get_content_type_for_model', get_content_type_for_model): return super(RestAdmin, self).render_change_form(*args, **kwargs)
def formfield_for_dbfield(self, db_field, **kwargs): with patch('django.db.models.ForeignKey', ToOneField): with patch('django.db.models.ManyToManyField', ToManyField): return super(RestAdminBase, self).formfield_for_dbfield(db_field, **kwargs)
def get_form(self, request, obj=None, **kwargs): with patch('django.forms.models.modelform_factory', restform_factory): return super(RestAdmin, self).get_form(request, obj, **kwargs)
def _get_foreign_key(*args, **kwargs): from restorm.fields import ToOneField with patch('django.db.models.ForeignKey', ToOneField): return _dj_get_foreign_key(*args, **kwargs)