Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Are multiple Generic Relations in one Model bad design?

I have a model with multiple Generic Relations that has become very complicated to use in my templates. The model is a 'Gig' or musical event that takes place at a 'Venue' and/or a 'Festival' and has a 'Musician' and/or an 'Ensemble'.

Where it gets complicated is that each 'Gig' has a presenter, promoter and an agent. These are setup as generic relations to other models such as 'PresenterCompany'. A Presenter company could be a promoter, presenter, or agent, or all of them for the same gig. Here are the models (simplified for ref):

class Gig(models.Model):
    description = models.CharField(max_length=100, blank=True)
    date = models.DateTimeField()
    venue = models.ForeignKey(Venue)
    festival = models.ForeignKey(Festival, blank = True, null=True)
    musician = models.ManyToManyField(Musician, blank=True)
    ensembles = models.ManyToManyField(Ensemble, blank = True)

    presenter_content_type = models.ForeignKey(ContentType,
        limit_choices_to={"model__in": ("Individual", "ArtsOrganization",'Presenter', "BookingAgent","Festival", "OtherOrganization","PresenterCompany", "Venue")}, related_name = "Presenter Type", verbose_name = "Presenter",blank=True, null=True)
    presenter_id = models.IntegerField(db_index=True, blank=True, null=True, verbose_name='Presenter ID')
    presenter = generic.GenericForeignKey('presenter_content_type','presenter_id')

    promoter_content_type = models.ForeignKey(ContentType,
        limit_choices_to={"model__in": ("Individual", "ArtsOrganization","BookingAgent","Presenter", "Festival", "OtherOrganization","PresenterCompany", "Venue")}, related_name = "promotor", verbose_name='Promoter Type', blank=True, null=True)
    promoter_id = models.IntegerField(db_index=True, blank=True, null=True, verbose_name='Promoter ID')
    promoter = generic.GenericForeignKey('promoter_content_type','promoter_id')

    agent_content_type = models.ForeignKey(ContentType,
        limit_choices_to={"model__in": ("Individual", "BookingAgent")}, related_name="agent", verbose_name='Agent Type', blank=True, null=True)
    agent_id = models.IntegerField(db_index=True, blank=True, null=True, verbose_name='Agent ID')
    agent = generic.GenericForeignKey('agent_content_type','agent_id')


class PresenterCompany(models.Model):
    name = models.CharField(max_length=70)
    address =GenericRelation(Address)
    presented_gig = GenericRelation('Gig',
        content_type_field='presenter_content_type',
        object_id_field='presenter_id',
        related_name='presenter_presented_gig'
    )

    promoted_gig = GenericRelation('Gig',
        content_type_field='promoter_content_type',
        object_id_field='promoter_id',
        related_name='presenter_promoted_gig'
    )
    booked_gig = GenericRelation('Gig',
        content_type_field='promoter_content_type',
        object_id_field='promoter_id',
        related_name='presenter_booked_gig'
    )

The main issue is that when I try to get all of the gigs for a presenter company, I have to write three different for loops for each role i.e. {% for gig in presentercompany.presented_gig.all %}, and so on... This seems like redundant code.

Is there a better way to structure this such as using intermediary models for presenter, promoter, and agent? Thanks for your advice!

like image 284
Nahanaeli Schelling Avatar asked Jul 16 '13 22:07

Nahanaeli Schelling


1 Answers

Generic relationships can definitely be hard to deal with. I would only use them when there is no other option.

In your case, I see a couple other options. You could have a ManyToMany relationship between PresenterCompany and Gig using a through table to specify the type of relationship (https://docs.djangoproject.com/en/2.0/topics/db/models/#extra-fields-on-many-to-many-relationships):

class Gig(models.Model):
    description = models.CharField(max_length=100, blank=True)
    date = models.DateTimeField()
    venue = models.ForeignKey(Venue)
    festival = models.ForeignKey(Festival, blank=True, null=True)
    musician = models.ManyToManyField(Musician, blank=True)
    ensembles = models.ManyToManyField(Ensemble, blank=True)


class PresenterCompanyGigRelationship(models.Model):
    gig = models.ForeignKey(Gig, on_delete=models.CASCADE)
    presenter_company = models.ForeignKey(
        'PresenterCompany', on_delete=models.CASCADE)
    relationship = models.CharField(
        max_length=10,
        choices=(
            ('presenter', 'Presenter'),
            ('promoter', 'Promoter'),
            ('agent', 'Agent'),
            ))


class PresenterCompany(models.Model):
    name = models.CharField(max_length=70)
    git = models.ManyToManyField(Gig, through=PresenterCompanyGigRelationship)
like image 199
synic Avatar answered Oct 05 '22 03:10

synic