4

I have a JSONField in my model, when I try to save 0 as value in json in django admin page, it saves value as null. How can I save zero as value? Django version: 2.1.7

Django admin page:

enter image description here

my JSONField:

lecturer_scores = JSONField(
        verbose_name=_("Lecturer Score"),
        validators=[validate_lecturer_score],
        default=dict,
        blank=True
    )

My input:

{
"score 1": 0,
"score 2": 5
}

it saves like:

{
"score 1": null,
"score 2": 5
}

Validator:

from django.core.validators import validate_integer
from django.core.exceptions import ValidationError

def validate_lecturer_score(value):
    for k, v in value.items():
        if v:
            validate_integer(v)
    for value in value.values():
        if value:
            if value < 0 or value > 100:
                raise ValidationError(_("Lecturer score for user's participance rate calculation should be between 0 and 100."))
9
  • what is your validate_lecturer_score i reproduced your code except the validator. And the field saved as 0 not null. Commented Apr 10, 2019 at 7:08
  • what is your database? i can't reproduce it using postgres imgur.com/a/ETBZVHF Commented Apr 10, 2019 at 7:26
  • my database is PostgreSQL Commented Apr 10, 2019 at 7:27
  • 1
    @BearBrown issue was in my signal, I call a property method and set the values. I check if value is empty set it as null. something like this lecturer_scores[key] = self.lecturer_scores.get(key) or None Commented Apr 10, 2019 at 7:52
  • 2
    please write the answer for other who will search the same issue. Commented Apr 10, 2019 at 7:55

1 Answer 1

0

issue was in my signal, I call a property method and set the values. I check if value is empty set it as null. something like this lecturer_scores[key] = self.lecturer_scores.get(key) or None

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.