Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to reference objects with Generic ForeignKey reference in Django
    text
    copied!<p>I have a Django app with three models which use ContentTypes and Generic to try and allow the Task model to have a ForeignKey to either of the other two models using <a href="https://stackoverflow.com/questions/881792/how-to-use-dynamic-foreignkey-in-django">How to use dynamic foreignkey in Django?</a> as a guide (although I'll admit that I have no clue what's going on with this approach).</p> <pre><code>class Task(models.Model): date = models.DateTimeField(null = True) day = models.IntegerField(null = True) task = models.CharField(max_length = 100, null = False) owner = models.CharField(max_length = 25, null = True) notes = models.CharField(max_length = 250, null = True) isDone = models.BooleanField(default = False, null = False) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() maintenance = generic.GenericForeignKey('content_type', 'object_id') class Maintenance(models.Model): startMonth = models.CharField(max_length = 15, null = False) startYear = models.IntegerField(null = False) location = models.CharField(max_length = 20, null = False) mode = models.CharField(max_length = 20, null = False) taskRef = generic.GenericRelation(Task) class Template(models.Model): name = models.CharField(max_length = 25, null = False) location = models.CharField(max_length = 20, null = False) mode = models.CharField(max_length = 20, null = False) taskRef = generic.GenericRelation(Task) </code></pre> <p>Normally if Task just has a normal ForeignKey to Maintenance, I can get all the Tasks connected to any given Maintenance with Maintenance.task_set.all() but with this dynamic foreignkey system that function doesn't work. Does anyone know of an alternative function call to achieve this affect?</p>
 

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