How to include related_names in a mixing for a model?

7 views
Skip to first unread message

Jonty Needham

unread,
Nov 4, 2016, 8:23:56 AM11/4/16
to django...@googlegroups.com
So I have one model, which uses a ContentType to point to a collections of models across separate apps.

I want to be able to "plug in" the functionality linking that model to the other models in the separate app. So we have the follwing:


class MyModelMixin(models.Model):
    """ Mixin to get the TheModel data into a MyModel
    ""
    updated_date = models.DateTimeField(null=True, blank=True)
    submission = GenericRelation(
        TheModel,
        related_query_name='the_related_name')
    class Meta:
        abstract = True



The main model model looks like this:

class TheModel(models.Model):
    """ This tracks a specific submission of an SAQ by a specific site
    """
    updated = models.DateTimeField()
    status = models.CharField(max_length=32, blank=True, null=True)
    final_score = models.DecimalField(
        decimal_places=2, max_digits=30, default=-1,
    )
    config = models.ForeignKey(Config, blank=True, null=True)
    content_type = models.ForeignKey(
        ContentType,
        blank=True,
        null=True)
    object_id = models.PositiveIntegerField(blank=True, null=True)
    my_model_instance = GenericForeignKey()

And the mixin is included in the MyModel models, which are available in many different apps.

so I've jsut realised that the related name on the GenericRelation will only work for one model (at least it appears to, and I've had issues with that before with related name clashes, which this would be. How should I get round this? Updating the related_name will require a migration, so is theer a way to do this dynamically using the mixin, or should it be hardcoded per app in which it's referenced.
Reply all
Reply to author
Forward
0 new messages