我需要一个最多可链接3个数据(相同类型)的模型。
这是一个例子:
我有候选人需要通过3个步骤才能被招募
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,)
每个步骤都有几个人的评论
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,)
Candidate
,Review
,CandidateStep
和ReviewStep
?
示例数据:
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()
答案 0 :(得分:1)
三个对象怎么样:
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)
如何限制为3个步骤?
在视图级别上。如果您要使用ModelFromset
选项,请使用{{1}}。