Replace field.rel.to with field.remote_field.model

Field.rel and Field.remote_field.to are removed in Django 2.x
This commit is contained in:
Santiago L 2021-04-22 14:44:47 +02:00
parent b6cf0c34f5
commit 97f1c7ef2b
9 changed files with 32 additions and 35 deletions

View File

@ -27,7 +27,7 @@ def create_local_address(sender, *args, **kwargs):
mbox = kwargs['instance']
local_domain = settings.MAILBOXES_LOCAL_DOMAIN
if not mbox.pk and local_domain:
Domain = Address._meta.get_field('domain').rel.to
Domain = Address._meta.get_field('domain').remote_field.model
try:
domain = Domain.objects.get(name=local_domain)
except Domain.DoesNotExist:

View File

@ -6,7 +6,7 @@ from orchestra.core import services
def get_related_object(origin, max_depth=2):
"""
Introspects origin object and return the first related service object
WARNING this is NOT an exhaustive search but a compromise between cost and
flexibility. A more comprehensive approach may be considered if
a use-case calls for it.
@ -16,12 +16,12 @@ def get_related_object(origin, max_depth=2):
if hasattr(field, 'ct_field'):
yield getattr(node, field.name)
for field in node._meta.fields:
if field.rel:
if field.remote_field:
try:
yield getattr(node, field.name)
except ObjectDoesNotExist:
pass
# BFS model relation transversal
queue = [[origin]]
while queue:

View File

@ -98,7 +98,7 @@ class ResourceAdmin(ExtendedModelAdmin):
""" filter service content_types """
if db_field.name == 'content_type':
models = [ model._meta.model_name for model in services.get() ]
kwargs['queryset'] = db_field.rel.to.objects.filter(model__in=models)
kwargs['queryset'] = db_field.remote_field.model.objects.filter(model__in=models)
return super(ResourceAdmin, self).formfield_for_dbfield(db_field, **kwargs)

View File

@ -342,7 +342,7 @@ def create_resource_relation():
pass
else:
related._meta.private_fields = [
field for field in related._meta.private_fields if field.rel.to != ResourceData
field for field in related._meta.private_fields if field.remote_field.model != ResourceData
]
for ct, resources in Resource.objects.group_by('content_type').items():

View File

@ -11,5 +11,4 @@ class VirtualDatabaseRelation(GenericRelation):
pks.append(obj.database_id)
if not pks:
return []
# TODO renamed to self.remote_field in django 1.8
return self.rel.to._base_manager.db_manager(using).filter(pk__in=pks)
return self.remote_field.model._base_manager.db_manager(using).filter(pk__in=pks)

View File

@ -24,7 +24,7 @@ class SoftwareService(plugins.Plugin, metaclass=plugins.PluginMount):
'http': (Website.HTTP, (Website.HTTP, Website.HTTP_AND_HTTPS)),
'https': (Website.HTTPS_ONLY, (Website.HTTPS, Website.HTTP_AND_HTTPS, Website.HTTPS_ONLY)),
}
name = None
verbose_name = None
form = SaaSPasswordForm
@ -34,7 +34,7 @@ class SoftwareService(plugins.Plugin, metaclass=plugins.PluginMount):
class_verbose_name = _("Software as a Service")
plugin_field = 'service'
allow_custom_url = False
@classmethod
@lru_cache()
def get_plugins(cls, all=False):
@ -48,18 +48,18 @@ class SoftwareService(plugins.Plugin, metaclass=plugins.PluginMount):
for cls in settings.SAAS_ENABLED_SERVICES:
plugins.append(import_class(cls))
return plugins
def get_change_readonly_fields(cls):
fields = super(SoftwareService, cls).get_change_readonly_fields()
return fields + ('name',)
def get_site_domain(self):
context = {
'site_name': self.instance.name,
'name': self.instance.name,
}
return self.site_domain % context
def clean(self):
if self.allow_custom_url:
if self.instance.custom_url:
@ -69,7 +69,7 @@ class SoftwareService(plugins.Plugin, metaclass=plugins.PluginMount):
raise ValidationError({
'custom_url': _("Custom URL not allowed for this service."),
})
def clean_data(self):
data = super(SoftwareService, self).clean_data()
if not self.instance.pk:
@ -88,10 +88,10 @@ class SoftwareService(plugins.Plugin, metaclass=plugins.PluginMount):
if errors:
raise ValidationError(errors)
return data
def get_directive_name(self):
return '%s-saas' % self.name
def get_directive(self, *args):
if not args:
instance = self.instance
@ -106,7 +106,7 @@ class SoftwareService(plugins.Plugin, metaclass=plugins.PluginMount):
website__domains__name=url.netloc,
website__account=account,
)
def get_website(self):
url = urlparse(self.instance.custom_url)
account = self.instance.account
@ -117,10 +117,10 @@ class SoftwareService(plugins.Plugin, metaclass=plugins.PluginMount):
directives__name=self.get_directive_name(),
directives__value=url.path,
)
def create_or_update_directive(self):
return helpers.create_or_update_directive(self)
def delete_directive(self):
directive = None
try:
@ -131,7 +131,7 @@ class SoftwareService(plugins.Plugin, metaclass=plugins.PluginMount):
return
if directive is not None:
directive.delete()
def save(self):
# pre instance.save()
if isinstalled('orchestra.contrib.websites'):
@ -139,11 +139,11 @@ class SoftwareService(plugins.Plugin, metaclass=plugins.PluginMount):
self.create_or_update_directive()
elif self.instance.pk:
self.delete_directive()
def delete(self):
if isinstalled('orchestra.contrib.websites'):
self.delete_directive()
def get_related(self):
return []
@ -152,7 +152,7 @@ class DBSoftwareService(SoftwareService):
db_name = None
db_user = None
abstract = True
def get_db_name(self):
context = {
'name': self.instance.name,
@ -161,15 +161,15 @@ class DBSoftwareService(SoftwareService):
db_name = self.db_name % context
# Limit for mysql database names
return db_name[:65]
def get_db_user(self):
return self.db_user
@cached
def get_account(self):
account_model = self.instance._meta.get_field('account')
return account_model.rel.to.objects.get_main()
return account_model.remote_field.model.objects.get_main()
def validate(self):
super(DBSoftwareService, self).validate()
create = not self.instance.pk
@ -192,7 +192,7 @@ class DBSoftwareService(SoftwareService):
raise ValidationError({
'name': e.messages,
})
def save(self):
super(DBSoftwareService, self).save()
account = self.get_account()

View File

@ -59,7 +59,7 @@ class ServiceAdmin(ChangeViewActionsMixin, admin.ModelAdmin):
""" Improve performance of account field and filter by account """
if db_field.name == 'content_type':
models = [model._meta.model_name for model in services.get()]
queryset = db_field.rel.to.objects
queryset = db_field.remote_field.model.objects
kwargs['queryset'] = queryset.filter(model__in=models)
if db_field.name in ['match', 'metric', 'order_description']:
kwargs['widget'] = forms.TextInput(attrs={'size':'160'})

View File

@ -12,8 +12,7 @@ class VirtualDatabaseRelation(GenericRelation):
pks.append(db_id)
if not pks:
return []
# TODO renamed to self.remote_field in django 1.8
return self.rel.to._base_manager.db_manager(using).filter(pk__in=pks)
return self.remote_field.model._base_manager.db_manager(using).filter(pk__in=pks)
class VirtualDatabaseUserRelation(GenericRelation):
@ -26,5 +25,4 @@ class VirtualDatabaseUserRelation(GenericRelation):
pks.append(db_id)
if not pks:
return []
# TODO renamed to self.remote_field in django 1.8
return self.rel.to._base_manager.db_manager(using).filter(pk__in=pks)
return self.remote_field.model._base_manager.db_manager(using).filter(pk__in=pks)

View File

@ -50,9 +50,9 @@ def get_model_field_path(origin, target):
if node == target:
return path
for field in node._meta.fields:
if field.rel:
if field.remote_field:
new_model = list(model)
new_model.append(field.rel.to)
new_model.append(field.remote_field.model)
new_path = list(path)
new_path.append(field.name)
queue.append((new_model, new_path))