1

Is there a more "Python/Django" way to query/filter objects by generic foreign key? I'm trying to get all FullCitation objects for a particular software, where is_primary is True.

I know I can't do this but I want to do something like this:

ct_supported = ContentType.objects.get(app_label="supportedprogram", model="software")
primary_citations = FullCitation.objects.filter(content_type__name=ct_supported, object_id__in='', is_primary=True)

models.py

class FullCitation(models.Model)
    # the software to which this citation belongs
    # either a supported software program or a non-supported software program

    limit = models.Q(app_label = 'myprograms', model = 'supportedprogram') | models.Q(app_label = 'myprograms', model = 'nonsupportedprogram') 
    content_type = models.ForeignKey(ContentType), limit_choices_to = limit, )
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    is_primary = models.BooleanField(help_text="Is this the Primary Citation for the software program?")

class NonSupportedProgram(models.Model):
    title = models.CharField(max_length=256, blank = True)
    full_citation = generic.GenericRelation('FullCitation')

class SupportedProgram(models.Model):
    title = models.CharField(max_length=256, blank = True)
    full_citation = generic.GenericRelation('FullCitation')
    # and a bunch of other fields.....

views.py # My current attempt

primary_citations = []
sw_citations = sw.full_citations.all()
    for x in sw_citations:
        if x.is_primary:
            primary_citations.append(x)
3
  • Quickfix: use a list comprehesion [cit for cit in sw.full_citations.all() if cit.is_primary]
    – Alvaro
    Feb 4, 2015 at 22:48
  • Thank you, @Alvaro! That definitely helps. Just wondering if there is a way to filter on the FullCitation object itself that would be more Django-ish...if not, I'll definitely use the list comprehension!
    – steph
    Feb 4, 2015 at 22:53
  • You can also use a generator comprehension, (x for x in y) to delay the database query and only perform it when neccessary.
    – user764357
    Feb 4, 2015 at 23:59

1 Answer 1

4

Comprehensions should be a last resort for filtering QuerySets. Far better to let them remain as QuerySets as long as you can. I think this is what you're looking for:

ct_supported = ContentType.objects.get_for_model(SupportedProgram))
primary_citations = FullCitation.objects.filter(content_type=ct_supported, is_primary=True)

Updated: If you want to filter for a specific SupportedProgram instance, do this:

my_supported = SupportedProgram.objects.get(id=instance_id_goes_here)
ct_supported = ContentType.objects.get_for_model(SupportedProgram))
primary_citations = FullCitation.objects.filter(content_object=my_supported, content_type=ct_supported, is_primary=True)
2
  • that's almost there but not quite (definitely in the vein of what I'm looking for!)- I need one more 'level' of filtering. I want to filter not just on the SupportedProgram models but on a specific instance of Supported Program. So, I want all FullCitations where the SupportedProgram is, specifically, 'Program1' and then, of those FullCitations, I want only those where is_primary=True....
    – steph
    Feb 5, 2015 at 15:24
  • @steph Updated to filter for a specific SupportedProgram instance.
    – dylrei
    Feb 5, 2015 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.