1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

#3892: Convert CUSTOMFIELD_MODELS to a Q object

This commit is contained in:
Jeremy Stretch
2020-01-15 16:00:54 -05:00
parent f8dad1744c
commit 9c4ab79bea
5 changed files with 55 additions and 26 deletions

View File

@ -1,23 +1,38 @@
from django.db.models import Q
# Models which support custom fields # Models which support custom fields
CUSTOMFIELD_MODELS = [ CUSTOMFIELD_MODELS = Q(
'circuits.circuit', Q(app_label='circuits', model__in=[
'circuits.provider', 'circuit',
'dcim.device', 'provider',
'dcim.devicetype', ]) |
'dcim.powerfeed', Q(app_label='dcim', model__in=[
'dcim.rack', 'device',
'dcim.site', 'devicetype',
'ipam.aggregate', 'powerfeed',
'ipam.ipaddress', 'rack',
'ipam.prefix', 'site',
'ipam.service', ]) |
'ipam.vlan', Q(app_label='ipam', model__in=[
'ipam.vrf', 'aggregate',
'secrets.secret', 'ipaddress',
'tenancy.tenant', 'prefix',
'virtualization.cluster', 'service',
'virtualization.virtualmachine', 'vlan',
] 'vrf',
]) |
Q(app_label='secrets', model__in=[
'secret',
]) |
Q(app_label='tenancy', model__in=[
'tenant',
]) |
Q(app_label='virtualization', model__in=[
'cluster',
'virtualmachine',
])
)
# Custom links # Custom links
CUSTOMLINK_MODELS = [ CUSTOMLINK_MODELS = [

View File

@ -33,7 +33,7 @@ class Migration(migrations.Migration):
migrations.AlterField( migrations.AlterField(
model_name='customfield', model_name='customfield',
name='obj_type', name='obj_type',
field=models.ManyToManyField(limit_choices_to=extras.models.get_custom_field_models, related_name='custom_fields', to='contenttypes.ContentType'), field=models.ManyToManyField(related_name='custom_fields', to='contenttypes.ContentType'),
), ),
migrations.AlterField( migrations.AlterField(
model_name='exporttemplate', model_name='exporttemplate',

View File

@ -115,7 +115,7 @@ class Migration(migrations.Migration):
migrations.AlterField( migrations.AlterField(
model_name='customfield', model_name='customfield',
name='obj_type', name='obj_type',
field=models.ManyToManyField(limit_choices_to=extras.models.get_custom_field_models, related_name='custom_fields', to='contenttypes.ContentType'), field=models.ManyToManyField(related_name='custom_fields', to='contenttypes.ContentType'),
), ),
migrations.AlterField( migrations.AlterField(
model_name='exporttemplate', model_name='exporttemplate',

View File

@ -0,0 +1,18 @@
# Generated by Django 2.2.8 on 2020-01-15 21:00
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('extras', '0035_deterministic_ordering'),
]
operations = [
migrations.AlterField(
model_name='customfield',
name='obj_type',
field=models.ManyToManyField(limit_choices_to=models.Q(models.Q(models.Q(('app_label', 'circuits'), ('model__in', ['circuit', 'provider'])), models.Q(('app_label', 'dcim'), ('model__in', ['device', 'devicetype', 'powerfeed', 'rack', 'site'])), models.Q(('app_label', 'ipam'), ('model__in', ['aggregate', 'ipaddress', 'prefix', 'service', 'vlan', 'vrf'])), models.Q(('app_label', 'secrets'), ('model__in', ['secret'])), models.Q(('app_label', 'tenancy'), ('model__in', ['tenant'])), models.Q(('app_label', 'virtualization'), ('model__in', ['cluster', 'virtualmachine'])), _connector='OR')), related_name='custom_fields', to='contenttypes.ContentType'),
),
]

View File

@ -192,16 +192,12 @@ class CustomFieldModel(models.Model):
return OrderedDict([(field, None) for field in fields]) return OrderedDict([(field, None) for field in fields])
def get_custom_field_models():
return model_names_to_filter_dict(CUSTOMFIELD_MODELS)
class CustomField(models.Model): class CustomField(models.Model):
obj_type = models.ManyToManyField( obj_type = models.ManyToManyField(
to=ContentType, to=ContentType,
related_name='custom_fields', related_name='custom_fields',
verbose_name='Object(s)', verbose_name='Object(s)',
limit_choices_to=get_custom_field_models, limit_choices_to=CUSTOMFIELD_MODELS,
help_text='The object(s) to which this field applies.' help_text='The object(s) to which this field applies.'
) )
type = models.CharField( type = models.CharField(