Search code examples
pythondjangomodels

Django Models: I want to link a specific attribute of a model to another model that is not the primary key. How would I do it?


I am relatively new with Django, and I need some help with my models.

My models.py for the paperAssign app looks like (excluding all the imports):

class Reviewer(models.Model):

    reviewerID = models.AutoField(primary_key=True)
    paper = models.ManyToManyField(Document)

    Select_Reviewer = models.OneToOneField(RegUser, default=None)

    def __str__(self):
        return "%s" % (self.Select_Reviewer)

and my models.py file for the review app looks like (excluding all the imports):

SCORE_CHOICES = (
    (1, '1'),
    (2, '2'),
    (3, '3'),
    (4, '4'),
    (5, '5'),
    (6, '6'),
    (7, '7'),
    (8, '8'),
    (9, '9'),
    (10, '10'),
)

CONFIDENCE_CHOICES = (
    (1, 'Not confident'),
    (2, 'Unsure'),
    (3, 'Somewhat confident'),
    (4, 'Moderately confident'),
    (5, 'Very confident'),
)


class Review(models.Model):
    comment = models.TextField()
    score = models.IntegerField(choices=SCORE_CHOICES)  
    confidence = models.IntegerField(choices=CONFIDENCE_CHOICES)
    paper = models.ForeignKey(Document)

    def __str__(self):
        return '%s' % (self.paper)

I need to connect the paper attribute of class Review to the paper attribute of class Reviewer. Is it possible to have a second primary key? Or how should I do this?

Thank you in advance!


Solution

  • Limiting the papers viewable by a reviewer doesn't require any connecting of fields; the relationships you have are perfectly sufficient. You simply need to filter the papers query; assuming you have a reviewer object, you can get all their assigned papers with reviewer.paper.all().