Incorporating many-to-many through model across multiple relationships

Hi community!,

I have a project that I am trying assign subitems of a model to different instances of another model, yet there are other relations (ForeignKey and another manytomany) in between the outcomes that I am looking.

Context: We are building an app that registers different measures and standards for a particular site. Multiple standards can be assigned to a measure, and each standard has associated subitems. Each site will have measures, and standards assigned to them but different sites need to be associated with different standard subitems.

I am almost 100% sure that I need to use a manytomany intermediate model. But all examples I have come across usually only relate to adding extra fields to two models, and not any more,

The models:

class Site(models.Model):
    site_location = models.CharField(max_length=1000, null=False)
    standards = models.ManyToManyField("Measure")

class Measure(models.Model):
    name = models.CharField(max_length=1000, null=False)
    standards = models.ManyToManyField("Standard")

class Standard(models.Model):
    name = models.CharField(max_length=1000, null=False)

class StandardItem(models.Model):
    name = models.CharField(max_length=1000, null=False)
    standard = models.ForeignKey(Standard)

Is it as easy as adding an intermediate model (i.e. am i overthinking this?) e.g.

class SiteStandards(models.Model):
    site = models.ForeignKey(Site, on_delete=models.CASCADE)
    standard_item = models.ForeignKey(StandardItem, on_delete=models.CASCADE)
   

Then when creating a site, I can assign the measure, standards and different sub-items to it, or am I thinking about this all wrong from a design perspective.

Any clarity you can offer would be much appreciated!

Ok, rephrasing this to ensure I understand what you’re trying to achieve:

Site:
    many-to-many relationship with Measure (the "Site.standards" field)
    many-to-many relationship with StandardItem (the "SiteStandards" model)

Measure:
    many-to-many relationship with Standard (the "Measure.standards" field)

StandardItem:
    many-to-one relationship with Standard (the "StandardItem.standard" field)

This is what you’ve defined so far.

Is there another relationship among these models that still needs to be described?

Hi Ken, thank you for your reply!

Sites can have many measures, measures can have many Standards but individual standards will have their own set of StandardItems (not shared between standards). However, indivudal sites will only need to be assigned some of the StandardItems within a Standard.

I hope this helps to clarify. Even though it feels like I am talking circles. Maybe it would help to give a more concrete example. In this context a Site is a building, that building will have a set of different safety measures (e.g. Emergency Lighting, Exit Signs etc). Those measures are associated with safety standards, that consist of sub-items. However, not all sub-items will be applicable to that building (but maybe another).

Cheers

Yes, I think we’re on the same page.

Your definitions appear to match what you’ve identified as the requirements, so I think you’re good from that perspective.

Assuming that’s accurate, is there still a question you need answered?

Possibly, Im still a little confused on how to only assign some sub-items to a particular site (through the measure and standard), or would the intermediate model i specified above handle that. That way when i assign a measure and standard, i can set the subitems through the intermediate model.

This model:

is the definition of a ManyToManyField between Site and StandardItem. (In other words, the exact same thing would occur if you created the ManyToManyField in either of those models.)

Ok fantastic! than you Ken for helping me understand this better.

Also just confirm, i would modify my StandardItem model to go through SiteStandards?

Much appreciated!