Monday, October 28, 2013

Querying across Generic Relations

I have a table, Credit, that I want to query based on a value several hops away. There's a CreditLineItems that has a Generic Foreign Key to one of two tables, each of which has a Foreign Key to the table I want to search. I've not as yet setup a reverse relation in the related tables.

My question is, given the structure below, how do I return a selection of Credits based on a value in Registration?

class Credit(models.Model):
""" Model queried """
    

class CreditLineItem(models.Model):
    credit = models.ForeignKey(Credit)
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')  #assumed values...
# The GenericForeignKey could point to
class Enrollment(models.Model):
    registration = models.ForeignKey(Registration, blank=True)

--OR--

class ProductSale(models.Model):
    registration = models.ForeignKey(Registration, blank=True)

class Registration(models.Model):
    registration_number =  models.IntegerField(db_index=True)

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BePoMz2s2qowuUgs%3DnBOcde6PGTZEEg1qMYJfos-dDE3VOUHA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment