Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could have the relationship on the <code>FinancialTransaction</code> using a Generic foreign key.</p> <p><a href="https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#id1" rel="nofollow">https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#id1</a></p> <pre><code>from django.db import models from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes import generic class FinatialTransation(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type', 'object_id') </code></pre> <p>Then the relationship exists in one place and there can only be 1.</p> <p>Then from the <code>FinancialTransaction</code> you check the object ID and the objects <code>ContentType</code> and look it up accordingly.</p> <pre><code>ft = FinancialTransaction.objects.get(...) thing = ft.content_type.get_object_for_this_type(id=ft.object_id) </code></pre> <p>Additionally you can then limit the GenericForeignKey to certain content types with:</p> <pre><code>class FinatialTransation(models.Model): limit = models.Q( models.Q(app_label='yourappsname', model='ThingOne') | models.Q(app_label='yourappsname', model='ThingTwo') | models.Q(app_label='yourappsname', model='ThingThree') ) content_type = models.ForeignKey(ContentType, limit_choices_to=limit) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type', 'object_id') </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload