#30650: Need QuerySet.extra for a where clause based on on a CharField value in 
a
second model
-------------------------------------+-------------------------------------
               Reporter:  Scott      |          Owner:  nobody
  Deerwester                         |
                   Type:  New        |         Status:  new
  feature                            |
              Component:  Database   |        Version:  2.2
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:  QuerySet.extra
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 I'm responding to
 "[https://docs.djangoproject.com/en/2.2/ref/models/querysets/#extra please
 file a ticket]" in the QuerySet documentation. I have a couple of models
 something like:

 {{{
     class ShippingStory(models.Model):
         ...
         story_type = models.CharField(...)
         status = models.CharField(...)
         orig_airport_code = models.CharField(...)
         dest_airport_code = models.CharField(...)
         orig_port_code = models.CharField(...)
         dest_port_code = models.CharField(...)
         ...

     class Airport(models.Model):
         name = models.CharField(...)
         iata_code = models.CharField(...)
         country = models.CharField(...)
 }}}

 I want to do the equivalent of:

 {{{
     SELECT shippingstory.* FROM shippingstory, airport
     WHERE
        ... shipping story-related selections
        AND
        airport.iata = shippingstory.orig_airport_code AND
        airport.country = 'US';
 }}}

 I'm unable to see how to do that without resorting to something like

 {{{
     subquery = {...} # fields related only to the ShippingStory model
     qs = ShippingStory.objects.filter(**subquery)
     qs = qs.extra(
         where='shippingstory.orig_airport_code=airport.iata AND
 airport.country=%s'
         where_params=(orig_country,),
         tables=('airport',)
     )
 }}}

 For a number of complicated reasons, I'm unable to express the respective
 fields in `ShippingStory` as `ForeignKey` fields.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30650>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" 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-updates/049.0192be0e72b78e91742fde4d48322abf%40djangoproject.com.

Reply via email to