I am trying to apply basic model inheritance based on the Django documentation. The end goal is to access the shared id of the Exam and Date models in the get_date_info view, which is called after the get_exam_info view. Perhaps there is a better way to do this than model inheritance, but this seemed the most straight forward to me.
Here is the relevant Model code:
class Exam(models.Model):
instructor_first_name = models.CharField(max_length=30)
instructor_last_name = models.CharField(max_length=30)
department = models.CharField(max_length=30, null=True)
course_name = models.CharField(max_length=30, null=True)
course_number = models.IntegerField(null=True)
section_number = models.IntegerField(null=True)
num_students = models.IntegerField(null=True)
calculator = models.CharField(max_length=30, blank=True)
notes = models.CharField(max_length=30, blank=True)
computer_exam = models.BooleanField(default=False)
scantron = models.BooleanField(default=False)
timed = models.BooleanField(default=False)
dictionary = models.BooleanField(default=False)
comment = models.CharField(max_length=300, blank=True)
class Date(Exam):
start_date = models.DateField()
end_date = models.DateField()
late_start_date = models.DateField(blank=True, null=True)
late_end_date = models.DateField(blank=True, null=True)
Here is the relevant view code:
def get_exam_info(request):
if request.method == 'POST':
exam_form = ExamForm(request.POST)
if exam_form.is_valid():
exam_form.save()
date_form = DateForm()
return render(request, 'date_info.html', {'date_form': date_form})
else:
exam_form = ExamForm()
return render(request, 'exam_info.html', {'exam_form': exam_form})
def get_date_info(request):
exam_request = Exam.objects.all()
if request.method == 'POST':
date_instance = Date()
date_form = DateForm(request.POST, instance=date_instance)
if date_form.is_valid():
date_form.save()
current_user_id = date_instance.exam_ptr_id
print(current_user_id)
return HttpResponseRedirect('/')
else:
date_form = DateForm()
return render(request, 'date_info.html', {'date_form': date_form})
Here is the result in the database: This is the Exam table
Some other things I tried: I created an abstract model that contains the shared id, then I made the Exam and Date classes subclasses of this abstract superclass. This gave the expected behavior in the database, but then the exam instances were not available in the views. I also tried many combinations of creating OneToOneFields to link the Exam and Date models, but none of these gave the desired behavior.
I've been trying to figure out why this happening for an embarrassingly long time. Any help would be appreciated!