Seems to me case-when and conditional expressions[0] would be a better fit 
for you.  Django selects all the fields of the models, except when values 
and values_list are used to specify the fields.  however, those methods do 
not populate model instances, because why woould they?  they only have a 
partial set of model data anyway, and the project expects that if you 
return a model instance, all of its fields are accessible as a whole as 
defined in the class definition.

[0] 
https://docs.djangoproject.com/en/4.1/ref/models/conditional-expressions/

On Thursday, February 2, 2023 at 6:26:05 PM UTC-5 thomas...@gmail.com wrote:

> Hello everyone,
>
> I realize that it is a pretty exotic feature, but is there a way to =
> generate SQL from a query set so that the fields can be a wildcard, when =
> multiple joins exist.
>
> Given this query:
>
> SELECT *
> FROM table1 t1
> JOIN table2 t2 on t1.id = t2.t1_id
> JOIN table3 t3 on t2.id = t3.t2_id
> WHERE (...)
>
>
> How can I use Django to rewrite the select as `SELECT t1.*` for example?
>
> My use-case is a set of nested models with a foreign key relationship 
> (t1 has many t2 has many t3) and I would like to query t2 but return
> either t1, t2 or t3 instances depending on the cases (and presumably t1,
> t2 and t3 are serializable as models in the ORM)
>
> Is this possible? So far, I looked at `values` and `values_list` but 
> there doesn't seem to be a documented way to do this.
>
> Any help appreciated, I know this group is helpful,
>
> -- Thomas
>

-- 
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 django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/6d05bf24-a402-4c5f-8167-a15d9362acefn%40googlegroups.com.

Reply via email to