models.py
class Courses(models.Model):
course_name=models.CharField(max_length=50)
course_price=models.IntegerField()
class Exam(models.Model):
exam_name=models.CharField(max_length=101)
course=models.ForeignKey(Courses,on_delete=models.CASCADE,default='python')
exam_time=models.DateTimeField()
views.py
def Examadd(request):
mycourses = Courses.objects.all()
context = {'mycourses': mycourses}
if request.method == 'POST':
newexam = request.POST.get('examname')
course = request.POST.get('courses')
examtime = request.POST.get('time')
new = Exam.objects.create(exam_name=newexam,course=course,exam_time=examtime)
new.save()
messages.success(request, "Course created successfully")
return redirect('Courselist')
return render(request,'addexam.html',context)
addexam.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Add New Exam</h1>
<form method="post">
{% csrf_token %}
<label>Examname:</label>
<input type="text" name="examname">
<label>Course:</label>
<select name="courses">
{% for i in mycourses %}
<option value={{i.id}}>{{i.course_name}}</option>
{% endfor %}
</select>
<label>Exam time and date:</label>
<input type="text" name="examtime">
<button type="submit">Add</button>
</form>
</body>
</html>
I am doing a project elearning.I want a dropdown list with courses and pass its ids to Exam table.course id is a foreign key.I want to pass the courseid to course column in Exam table.By this code I gets error as,Cannot assign "'1'": "Exam.course" must be a "Courses" instance.
CodePudding user response:
At first, create()
method doesn't require save()
method to be called, and you can directly assign course id in the Exam
model, so the view should be:
def Examadd(request):
mycourses = Courses.objects.all()
context = {'mycourses': mycourses}
if request.method == 'POST':
newexam = request.POST.get('examname')
course = request.POST.get('courses')
examtime = request.POST.get('time')
new = Exam.objects.create(exam_name=newexam,course__id=course,exam_time=examtime)
messages.success(request, "Course created successfully")
return redirect('Courselist')
return render(request,'addexam.html',context)
CodePudding user response:
You should assign the primary key to course_id
, so:
def Examadd(request):
mycourses = Courses.objects.all()
context = {'mycourses': mycourses}
if request.method == 'POST':
newexam = request.POST.get('examname')
course = request.POST.get('courses')
examtime = request.POST.get('time')
new = Exam.objects.create(
exam_name=newexam, course_id=course, exam_time=examtime
)
messages.success(request, "Course created successfully")
return redirect('Courselist')
return render(request, 'addexam.html', context)
I would however advise to use a ModelForm
, this makes saving the data easier and less error-prone, but will also do proper validation: for example checking that time
is indeed passed, and that it is formatted as a correct datetime string.
Note: normally a Django model is given a singular name, so
Course
instead of.Courses
Note: Normally model fields have no prefix with the name of the model. This makes queries longer to read, and often want uses inheritance of (abstract) models to inherit fields, so using a prefix would make it less reusable. Therefore it might be better to rename your field
tocourse_name
name
.