0

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

And this is the Date 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!

  • If the goal is to access the id of the exam from the date table, why not make the exam a foreign key in the date model? That way you will always have access to the linked exam for any date. https://docs.djangoproject.com/en/3.0/ref/models/fields/#foreignkey – Josh C Apr 01 '20 at 18:07
  • Thanks for the response... I tried this, but then date_instance returns Date object (None) when it is called in the get_date_info view. To add a little more context, I want to access the current exam and date objects in the get_date_info view so that I can count the number of exams that occur in the date range chosen. I'll then send a list back to the date picker to block out dates that are full. – Michael Snyder Apr 02 '20 at 15:15

0 Answers0