Jelajahi Sumber

Create CachedValueField to contain search specific lookups

kkthxbye-code 3 tahun lalu
induk
melakukan
eed1b8f412

+ 7 - 0
netbox/extras/fields.py

@@ -0,0 +1,7 @@
+from django.db.models import TextField
+
+class CachedValueField(TextField):
+    """
+    Currently a dummy field to prevent custom lookups being applied globally to TextField.
+    """
+    pass

+ 2 - 2
netbox/extras/lookups.py

@@ -1,5 +1,5 @@
 from django.db.models import CharField, TextField, Lookup
-
+from .fields import CachedValueField
 
 class Empty(Lookup):
     """
@@ -27,4 +27,4 @@ class NetContainsOrEquals(Lookup):
         return 'CAST(%s as inet) >>= %s' % (lhs, rhs), params
 
 CharField.register_lookup(Empty)
-TextField.register_lookup(NetContainsOrEquals)
+CachedValueField.register_lookup(NetContainsOrEquals)

+ 19 - 0
netbox/extras/migrations/0085_change_cachedvalue_value_type.py

@@ -0,0 +1,19 @@
+# Generated by Django 4.1.6 on 2023-02-07 08:21
+
+from django.db import migrations
+import extras.fields
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('extras', '0084_staging'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='cachedvalue',
+            name='value',
+            field=extras.fields.CachedValueField(),
+        ),
+    ]

+ 2 - 1
netbox/extras/models/search.py

@@ -4,6 +4,7 @@ from django.contrib.contenttypes.models import ContentType
 from django.db import models
 
 from utilities.fields import RestrictedGenericForeignKey
+from ..fields import CachedValueField
 
 __all__ = (
     'CachedValue',
@@ -36,7 +37,7 @@ class CachedValue(models.Model):
     type = models.CharField(
         max_length=30
     )
-    value = models.TextField()
+    value = CachedValueField()
     weight = models.PositiveSmallIntegerField(
         default=1000
     )