Pierre de LESPINAY
Pierre de LESPINAY

Reputation: 46238

Django - Which model for a limited related data

I need a model that will be linked to a maximum of 3 data (of the same type).

Here is an example:
I have candidates that need to pass 3 steps to be recruited

class Candidate(models.Model):
  user = models.ForeignKey(User)
  step1 = models.TextField(blank=True, null=True,)
  step2 = models.TextField(blank=True, null=True,)
  step3 = models.TextField(blank=True, null=True,)

For each step there is a review by several persons

class Review(models.Model):
  candidate = models.ForeignKey(Candidate)
  reviewer = models.ForeignKey(User)
  step1 = models.TextField(blank=True, null=True,)
  step2 = models.TextField(blank=True, null=True,)
  step3 = models.TextField(blank=True, null=True,)

Example data:

obj, created = Candidate.objects.get_or_create(
  user = SelectedCandidate
, defaults = {'step1': '', 'step2': '', 'step3': ''}
)
obj.step1 = 'I\'m really motivated'
obj.step2 = 'I\'m able to do this job'
obj.save()

obj, created = Review.objects.get_or_create(
  user = request.user
, defaults = {'step1': '', 'step2': '', 'step3': ''}
)
obj.step1 = 'He seems over motivated'
obj.save()

Upvotes: 0

Views: 82

Answers (1)

DrTyrsa
DrTyrsa

Reputation: 31991

What about three objects:

class Candidate(models.Model):
    name = models.CharField(max_length=50)

class Step(model.Model):
    candidate = models.ForeignKey(Candidate)
    text = models.TextField(blank=True)

class Review(models.Model):
    reviewer = models.ForeignKey(User)
    step =  models.OneToOneField(Step)
    text = models.TextField(blank=True)

how can I limit to 3 steps ?

On view level. If you are going to use ModelFromset, using max_num option.

Upvotes: 1

Related Questions