django: get() 返回了不止一个自由职业者——它返回了 3
django: get() returned more than one Freelancers -- it returned 3
我正在尝试将一名自由职业者分配给特定的演出,但它显示 get() 返回了不止一个自由职业者——它返回了 3 个!。我已经尝试让登录的自由职业者尝试创建这样的 git freelancer = get_object_or_404(Freelancers, user=user)
并且在我保存表格之前我分配了这样的值 new_form.creator = freelancer
.
views.py
@login_required
def create_gig(request):
user = request.user
freelancer = get_object_or_404(Freelancers, user=user)
if request.method == "POST":
form = CreateGig(request.POST, request.FILES)
if form.is_valid():
new_form = form.save(commit=False)
new_form.user = request.user
new_form.creator = freelancer
new_form.slug = slugify(new_form.title)
new_form.save()
messages.success(request, f'Gig Created Successfully, Would be Live Soon')
return redirect('freelance:listings')
else:
form = CreateGig()
context = {
'form': form
}
return render(request, 'freelance/create.html', context)
models.py
class Gigs(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='gig_user')
creator = models.ForeignKey(Freelancers, on_delete=models.CASCADE, related_name='gig_creator')
title = models.CharField(max_length=1000, null=True, blank=True, verbose_name="Enter what you will do", default=" I will ")
在我的代码中,它正在获取一个已登录的用户实例,然后通过将自由职业者分配给创建的表单来创建演出。
@login_required
def create_gig(request):
freelancer = Freelancers.objects.get(user = request.user )
if request.method == "POST":
form = CreateGig(request.POST, request.FILES)
if form.is_valid():
new_form = form.save(commit=False)
new_form.creator = freelancer
new_form.slug = slugify(new_form.title)
new_form.save()
messages.success(request, f'Gig Created Successfully, Would be Live Soon')
return redirect('freelance:listings')
else:
form = CreateGig()
context = {
'form': form
}
return render(request, 'freelance/create.html', context)
你也不需要在你看来这一行:
new_form.user = request.user
试试这个,如果您还需要任何帮助,请告诉我
我正在尝试将一名自由职业者分配给特定的演出,但它显示 get() 返回了不止一个自由职业者——它返回了 3 个!。我已经尝试让登录的自由职业者尝试创建这样的 git freelancer = get_object_or_404(Freelancers, user=user)
并且在我保存表格之前我分配了这样的值 new_form.creator = freelancer
.
views.py
@login_required
def create_gig(request):
user = request.user
freelancer = get_object_or_404(Freelancers, user=user)
if request.method == "POST":
form = CreateGig(request.POST, request.FILES)
if form.is_valid():
new_form = form.save(commit=False)
new_form.user = request.user
new_form.creator = freelancer
new_form.slug = slugify(new_form.title)
new_form.save()
messages.success(request, f'Gig Created Successfully, Would be Live Soon')
return redirect('freelance:listings')
else:
form = CreateGig()
context = {
'form': form
}
return render(request, 'freelance/create.html', context)
models.py
class Gigs(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='gig_user')
creator = models.ForeignKey(Freelancers, on_delete=models.CASCADE, related_name='gig_creator')
title = models.CharField(max_length=1000, null=True, blank=True, verbose_name="Enter what you will do", default=" I will ")
在我的代码中,它正在获取一个已登录的用户实例,然后通过将自由职业者分配给创建的表单来创建演出。
@login_required
def create_gig(request):
freelancer = Freelancers.objects.get(user = request.user )
if request.method == "POST":
form = CreateGig(request.POST, request.FILES)
if form.is_valid():
new_form = form.save(commit=False)
new_form.creator = freelancer
new_form.slug = slugify(new_form.title)
new_form.save()
messages.success(request, f'Gig Created Successfully, Would be Live Soon')
return redirect('freelance:listings')
else:
form = CreateGig()
context = {
'form': form
}
return render(request, 'freelance/create.html', context)
你也不需要在你看来这一行:
new_form.user = request.user
试试这个,如果您还需要任何帮助,请告诉我