F.objects.select_related('g__n') looks a bit convoluted to me.

You can already do something like F.objects.select_related('g').only('g__n',
*[f.name for f in F._meta.get_fields()]) and write a small helper function
for the list comprehension to avoid typing.

Also I'd say it's generally a rare case that such an optimization is worth
it, so it's probably not useful to add ORM shortcut syntax for it.

On 26 January 2018 at 17:24, Дилян Палаузов <dpa-dja...@aegee.org> wrote:

> Hello,
>
> Provided:
>
>     class M(models.Model):
>         n = models.IntegerField()
>         r = models.IntegerField()
>         t = models.IntegerField()
>         s = models.IntegerField()
>         u = models.IntegerField()
>
>     class F(models.Model):
>         g = models.ForeignKey(M, on_delete=models.CASCADE)
>         a = models.IntegerField()
>         b = models.IntegerField()
>         c = models.IntegerField()
>         d = models.IntegerField()
>         e = models.IntegerField()
>
>
> I want to obtain with a queryset all fields from F and M.n, but not the
> other fields of M like M.r, so that a single SELECT is generated, that
> doesn't pick unnecessary columns.
>
> F.objects.select_related('g').defer('g__r', 'g__t', 'g__s', 'g__u') does
> work, but I don't like it, as I want to say only M.n has to be obtained,
> and not the others.
>
> prefetch_related() is also suboptimal, as it creates two SQL queries
> instead of one. only() does not let me specify 'All fields of F'.
>
> So here neither only() nor defer() nor prefetch_related() are good.
>
> F.objects.select_related('g__n') would fit from logic, but does not work
> (django.core.exceptions.FieldError: Non-relational field given in
> select_related: 'n'. Choices are: (none))
>
> I propose teaching select_related() to accept the above expression.
>
> Confer https://code.djangoproject.com/ticket/29072 .
>
> Regards
>   Дилян
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers  (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-developers+unsubscr...@googlegroups.com.
> To post to this group, send email to django-developers@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-developers.
> To view this discussion on the web visit https://groups.google.com/d/ms
> gid/django-developers/d9a58b37-d084-2efe-33b0-4cd9b8289796%40aegee.org.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Adam

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CAMyDDM3bpJnz%2BPNux3XG0VEWJV0vMjqJ2ex_uQyU5nLL3wjS1A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to