Tuesday, October 30, 2018

Left outer join rather than subquery.


Suppose you have a setup like this:

     class Journal(models.Model):
          title = models.CharField(max_length=200)


     issn_type_choices = (
         ('E', 'Electronic'),
         ('P', 'Print'),
     }

     class Issn(models.Model):
          ELECTRONIC='E'
          PRINT='P'

          journal = models.ForeignKey(Journal)
          issn_type = models.CharField(max_length=1)
          issn = models.CharField(max_length=10)


I can express annotations to assure I get botha  Physical and Print ISSN:

issn_p=Issn.objects.filter(issn_type='E', journal_id=OuterRef('id'))
issn_e=Issn.objects.filter(issn_type='P', journal_id=OuterRef('id'))
queryset = Journal.objects.annotate(issn_print=Subquery(issn_p), issn_electronic=Subquery(issn_e)).filter(issn_print__isnull=False, issn_electronic__isnull=False)

However, is there anyway for me to express two OneToOneField relationships from Issn to Journal to make sure a constraint is added at the database level and get easier querysets?


Thanks,

-Dan

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/116bf684-237a-4a33-a218-8c0cf69e3091%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment