链接此表的最佳方法是什么,对不起,我是Django的新手,我需要一点帮助,我得到了3个表:
所以我希望用户申请尚未发布的职位,并且只能申请一次。
所以我现在得到的是:
class Applied(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
job = models.ForeignKey(Job, on_delete=models.CASCADE)
class Meta:
unique_together = ('userenter code here', 'job',)
这有效,但是如果我尝试两次申请同一份工作,则会抛出错误。
duplicate key value violates unique constraint "job_applied_user_id_job_id_c38a42a4_uniq"
DETAIL: Key (user_id, job_id)=(1, 6) already exists.
这是我的模特
class Job(models.Model):
title = models.CharField(max_length=100)
author = models.ForeignKey(User, on_delete=models.CASCADE)
cost = models.DecimalField(max_digits=25, decimal_places=4)
这是我的观点:
def apply(request, id):
print "the %s has applied " %(request.user.username)
job = Job.objects.get(pk=id)
if job.author == request.user:
messages.warning(request, 'You cannot apply to jobs you have publish')
else:
user = User.objects.get(pk=request.user.id)
applied = Applied(user = user, job = job)
if applied.not_applied_before():
applied.save()
messages.success(request, 'applied to job success')
else:
messages.info(request, 'You apply to this job already')