* owner: nobody => Aman Pandey
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/34013#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by Aman Pandey):
"It seems there is a lack of support JSONField in order_by queryset."
this is not true as JSONField based example queryset is working fine with
order_by method
eg:
""""
class Dog(models.Model):
name = models.CharField(max_length=200)
data = models.JSONField(null=True)
def __str__(self):
return self.name
""""
>>> dogs = Dog.objects.all().order_by("data")
>>> dogs
<QuerySet [<Dog: stella>, <Dog: tuffy>, <Dog: shifu>, <Dog: milo>]>
this is working ..
can someone please tell me about this model attribute how does this
assigned to the object
or wheather i should start with annotate class?
--
Ticket URL: <https://code.djangoproject.com/ticket/34013#comment:3>
Comment (by Aman Pandey):
i think the issue is JSONObject returns a JSONField and since it's
annotated JSONField doesn't get populated with the details of model it
belongs to
since field generally are called in a model so they get populated with
details of that model , here JSONField didn't get any details , including
"self. model"
which causes an error .
but this is only happening with "___ " look up call in order_by , but not
with overall call , so i am little confused where to look forward to , to
fix it , JSONField, JSONObejct , order_by or Lookup ?
--
Ticket URL: <https://code.djangoproject.com/ticket/34013#comment:4>
* status: assigned => closed
* resolution: => fixed
Comment:
i was trying to populate the Model and freshly created JSONField with
each other's info like trying to populate JSONField.model with model info
and
model._meta with JSONField info while the execution of annotate->
order_by-> add_ordering ->names_to_path (where lookup is processed ) but
that approach is very naive and making me fall deeper.
--
Ticket URL: <https://code.djangoproject.com/ticket/34013#comment:5>
* status: closed => new
* resolution: fixed =>
--
Ticket URL: <https://code.djangoproject.com/ticket/34013#comment:6>
Comment (by Rodolfo Valentín Becerra García):
Try this
{{{
Model.objects.all().annotate(
json_field=functions.JSONObject(
test_pk=models.Value(100) + models.F("pk"),
),
test_pk_from_json_field=KeyTextTransform(
"test_pk", "json_field"
)
)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34013#comment:7>
Comment (by saadullahaleem):
Is this issue still valid? I'm trying to find a good first issue to create
a PR for and did some digging here.
It looks like a `model` is attached to a `Field` object using the
`contribute_to_class` method.
It seems that the `contribute_to_class` method is called when fields are
added to the model class during its creation or when fields are added
dynamically. In this case, we're not adding or modifying fields in the
model class; we're only working with the queryset and computing new values
based on existing fields. The queryset annotations do not modify the model
class itself, so `contribute_to_class` is not called in this context.
--
Ticket URL: <https://code.djangoproject.com/ticket/34013#comment:8>
Comment (by Eugene Morozov):
I haven't tested this problem with the latest versions of Django 4.2 and
postgres 3. Nonetheless, I don't believe that the issue is linked to the
modifications made in Django 4.2.
So, you can try to run the code from the example and I think it will be
still not working.
--
Ticket URL: <https://code.djangoproject.com/ticket/34013#comment:9>
Comment (by saadullahaleem):
Replying to [comment:9 Eugene Morozov]:
> I haven't tested this problem with the latest versions of Django 4.2 and
postgres 3. Nonetheless, I don't believe that the issue is linked to the
modifications made in Django 4.2.
>
> So, you can try to run the code from the example and I think it will be
still not working.
>
Thanks for the quick response.
I was able to reproduce it on the main branch but to fix it, a model would
need to be attached to the JSONField inside the JSONObject method but it
can't be done since annotations don't modify the class itself. So it seems
like it might require a big architectural change for this to work.
--
Ticket URL: <https://code.djangoproject.com/ticket/34013#comment:10>