'Using abstraction with models for a generic ManyToManyField

I'm trying to make a sort of resume builder app, and rather than having explicit many-to-many relations between the "resume" model and the "work experience", "education", "skills", etc models, I want to do a sort of generic "Resume Item" model that points to a specific object in one of those tables.

Each of those types of items are their own model with their own set-in-stone structure.

I came across an answer here explaining that you can create a common item model, and then the specific models should reference it like so:


class ResumeItem(models.Model):
    pass

class Resume(models.Model):
    title = models.CharField(max_length=150, null=False, blank=False)
    items = models.ManyToManyField(ResumeItem, through=ResumeItemOrder)

class WorkExperience(models.Model):
    resume_item = models.OneToOneField(ResumeItem, on_delete=models.CASCADE)
    company = ...
    start_date = ...
    ...

class Skill(models.Model):
    resume_item = models.OneToOneField(ResumeItem, on_delete=models.CASCADE)
    name = ...

class ResumeItemOrder(models.Model):
    resume = models.ForeignKey(Resume)
    resumeItem = models.ForeignKey(ResumeItem)
    order = models.IntegerField() # the order in which this item will be rendered on the resume

For example, a resume will be rendered with ResumeItems 1, 2, and 3.

Item 1 is a work experience, item 2 is a skill, and item 3 is another work experience.

Ideally, the order of these items could be swapped around.

I'm not sure how I can query from the ResumeItem to obtain the specific items from their respective models. How does it know which model it should be querying and how can I specify the order in which these ResumeItems should reside in the Resume itself?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source