Reputation: 43
How do I access savedata.project_detail in views.py? Each user will have as many projects as he wants. Each project will have one unique primary key. I wan to access that primary key in def checklist(request) function. I only get model attribute "checked_value" from html template. The other attribute "project_detail" should automatically fetch primary key pf model "project". But I don't know how to fetch primary key of model project in def checklist function. If anyone can help here.
Here is models.py
class project(models.Model):
project_name = models.CharField(max_length = 20)
date = models.DateField(("Date"), default=datetime.date.today)
user = models.ForeignKey(User, on_delete=models.CASCADE)
class check_done(models.Model):
checked_value = models.CharField(max_length = 200)
project_detail = models.ForeignKey(project, to_field="id", db_column="project_detail", on_delete=models.CASCADE)
Here is views.py
def show(request):
log_user = request.user
projects = project.objects.filter(user=log_user)
return render(request, 'project.html',{'p':projects})
@login_required(login_url='/login/')
def add(request):
if request.method == "POST":
data = request.POST['data']
new = project(project_name = data, user=request.user)
new.save()
return render(request, 'addproject.html')
else:
return render(request, 'addproject.html')
@login_required(login_url='/login/')
def checklist(request):
if request.method == "POST":
savedata = check_done()
savedata.project_detail = ??
print(savedata.project_detail)
savedata.checked_value = request.POST.get('abc')
savedata.save()
return render(request, 'checklist.html')
else:
return render(request, 'checklist.html')
Upvotes: 2
Views: 2500
Reputation: 186
You need to assign a project model to savedata.project_detail attribute:
@login_required(login_url='/login/')
def checklist(request):
if request.method == "POST":
savedata = check_done()
savedata.project_detail = project.objects.get(id=request.POST['project_detail_id'])
savedata.checked_value = request.POST.get('abc')
savedata.save()
return render(request, 'checklist.html')
else:
return render(request, 'checklist.html')
Upvotes: 1
Reputation: 363
I think you could create or fetch project
object which you want to associate with the the savedata
object and assign it directly to the project_detail
attribute, or you could assign the project primary key to the project_detail_id
attribute.
Upvotes: 0