Skip to content

Commit a763c2b

Browse files
committed
[models] Replaced JSONField with Django's built-in #600
- Replaced third-party jsonfield.JSONField with Django's built-in models.JSONField in all model definitions - Updated RadiusBatch.prefix_add method to directly assign user_credentials list instead of using json.dumps() - Added migration 0043 to replace JSONField in model definitions - Added migration 0044 to convert existing double-encoded JSON data in user_credentials field (data migration in separate file) - Used .iterator() in data migration for memory efficiency - Removed jsonfield dependency from setup.py - Updated test migrations to use Django's JSONField - Removed unused json import Closes #600
1 parent fc948b9 commit a763c2b

5 files changed

Lines changed: 68 additions & 10 deletions

File tree

openwisp_radius/base/models.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import csv
22
import ipaddress
3-
import json
43
import logging
54
import os
65
import string
@@ -18,12 +17,11 @@
1817
from django.core.exceptions import ObjectDoesNotExist, ValidationError
1918
from django.core.mail import send_mail
2019
from django.db import models
21-
from django.db.models import ProtectedError, Q
20+
from django.db.models import JSONField, ProtectedError, Q
2221
from django.utils import timezone
2322
from django.utils.crypto import get_random_string
2423
from django.utils.timezone import now
2524
from django.utils.translation import gettext_lazy as _
26-
from jsonfield import JSONField
2725
from model_utils.fields import AutoLastModifiedField
2826
from openwisp_notifications.signals import notify
2927
from phonenumber_field.modelfields import PhoneNumberField
@@ -1021,7 +1019,7 @@ def prefix_add(self, prefix, n, password_length=BATCH_DEFAULT_PASSWORD_LENGTH):
10211019
for user in users_list:
10221020
user.full_clean()
10231021
self.save_user(user)
1024-
self.user_credentials = json.dumps(user_credentials)
1022+
self.user_credentials = user_credentials
10251023
self.full_clean()
10261024
self.save()
10271025

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Generated by Django 5.2.5 on 2025-10-22 18:34
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("openwisp_radius", "0042_set_existing_batches_completed"),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name="organizationradiussettings",
15+
name="sms_meta_data",
16+
field=models.JSONField(
17+
blank=True,
18+
help_text=(
19+
"Additional configuration for SMS backend in JSON format "
20+
"(optional, leave blank if unsure)"
21+
),
22+
null=True,
23+
verbose_name="SMS meta data",
24+
),
25+
),
26+
migrations.AlterField(
27+
model_name="radiusbatch",
28+
name="user_credentials",
29+
field=models.JSONField(blank=True, null=True, verbose_name="PDF"),
30+
),
31+
]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Generated by Django 5.2.5 on 2025-11-18 00:00
2+
3+
import json
4+
5+
from django.db import migrations
6+
7+
8+
def convert_user_credentials_data(apps, schema_editor):
9+
"""
10+
Convert existing double-encoded JSON strings in user_credentials field
11+
to proper JSON objects for Django's built-in JSONField.
12+
"""
13+
RadiusBatch = apps.get_model("openwisp_radius", "RadiusBatch")
14+
for batch in RadiusBatch.objects.exclude(user_credentials__isnull=True).iterator():
15+
if isinstance(batch.user_credentials, str):
16+
batch.user_credentials = json.loads(batch.user_credentials)
17+
batch.save(update_fields=["user_credentials"])
18+
19+
20+
class Migration(migrations.Migration):
21+
22+
dependencies = [
23+
(
24+
"openwisp_radius",
25+
"0043_alter_organizationradiussettings_sms_meta_data_and_more",
26+
),
27+
]
28+
29+
operations = [
30+
migrations.RunPython(
31+
convert_user_credentials_data, reverse_code=migrations.RunPython.noop
32+
),
33+
]

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
"weasyprint>=65,<67",
5050
"dj-rest-auth>=6.0,<7.1",
5151
"django-sendsms~=0.5.0",
52-
"jsonfield~=3.1.0",
5352
"django-private-storage~=3.1.0",
5453
"django-ipware>=5.0,<7.1",
5554
"pyrad~=2.4",

tests/openwisp2/sample_radius/migrations/0002_initial_openwisp_app.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import django.core.validators
88
import django.db.models.deletion
99
import django.utils.timezone
10-
import jsonfield.fields
1110
import model_utils.fields
1211
import private_storage.fields
1312
import private_storage.storage.files
@@ -543,7 +542,7 @@ class Migration(migrations.Migration):
543542
),
544543
(
545544
"sms_meta_data",
546-
jsonfield.fields.JSONField(
545+
models.JSONField(
547546
blank=True,
548547
help_text=(
549548
"Additional configuration for SMS backend in JSON format"
@@ -695,9 +694,7 @@ class Migration(migrations.Migration):
695694
),
696695
(
697696
"user_credentials",
698-
jsonfield.fields.JSONField(
699-
blank=True, null=True, verbose_name="PDF"
700-
),
697+
models.JSONField(blank=True, null=True, verbose_name="PDF"),
701698
),
702699
(
703700
"expiration_date",

0 commit comments

Comments
 (0)