I created a ticket <https://code.djangoproject.com/ticket/31231> about the 
similliar problem, but after answer I don't sure that it's bug. 
Nevertheless, I think It looks contradictory, because you use python None 
in case of creating, but {{{ __isnull=True }}} in case of filtering.
In case, that I described bellow, it also looks strange. 
TestModel.objects.create(json_field=None) creates SQL NULL, but 
TestModel.objects.create(json_field={'key': None}) creates JSON NULL.
May be this behaviour needs to change? Or is it better to extend the 
documentation?
Is this API allow to create a field with just JSON NULL value?


class TestJsonbNull(TestCase):
    def setUp(self):
        TestModel.objects.create(json_field=None)
        TestModel.objects.create(json_field={'key': None})

    # test passed
    def test_1(self):
        self.assertTrue(TestModel.objects.filter(json_field=None).exists())
    
    # test failed
    def test_2(self):
        self.assertTrue(TestModel.objects.filter(json_field__key=None).
exists())

вторник, 12 июля 2016 г., 18:56:47 UTC+5 пользователь [email protected] 
написал:
>
> I believe I found a limitation in the Django jsonb support, but I'm really 
> not sure how the framework could fix it beside adding a limitation section 
> in the documentation.
>
> Basically, it is possible to fetch models with json 'null' values but it 
> is not possible to save them back (Django converts the json 'null' value to 
> sql NULL value).
>
> Assume the following model:
>
> class Container(models.Model):
>     data = JSONField(blank=False, null=False) # emphasis on null=False
>
>
> In PostgreSQL, it is perfectly reasonable to do this:
>
> INSERT INTO container VALUES (1, 'null');
>
>
> If you fetch the row in Django, you will get this:
>
> container = Container.objects.get(pk=1)
>
> assert container.data is None # passes
>
> container.save() # raises IntegrityError because it tries to save NULL in 
> a NOT NULL column.
>
>
> This is because Django has no idea whether you want to store None or 
> 'null'::jsonb.
>
>
> I'm not sure if this limitation was discussed elsewhere. I found a 
> somehow-related ticket here: https://code.djangoproject.com/ticket/25718.
>
>
> Barthélémy
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e2f01198-13a5-4f4b-9707-3cecf00e06e7%40googlegroups.com.

Reply via email to