Can you use formsets for a table grid?

We want to create a web page where students can give a grade for the lessons they have received.They can grade the lessons for each day of the week. So they can grade the lessons of a particular course. These lessons may have been given on any day of the week, like this:

The database contains the following tables:

class Course(models.Model):
	title = models.CharField(max_length=88, null=True)
	startdate = models.DateField()
	enddate = models.DateField()
GivenGrades
	course = models.ForeignKey(Course, on_delete=models.Cascade)
	date = models.DateField()
	grade = models.IntegerField(null=True)

Each cell in the grid is actually a form on itself, so we are considering using a formset construction, but how to shape it? We are faced with the problem that not all courses on each day have a grade. Also, the question with formsets is how to get the titles of courses per line on the form.

Schould we use formsets or is there a better way to get this done? And is there an example somewhere of how to achieve a grid-like formset like this?

Thanks in advance

I’m not sure I’d try to use a formset here - I guess it’s do-able, but I’m not sure the UX is what you’d be looking for.
This is a case where I think I’d try to find a JavaScript-based grid tool - perhaps something like the jQuery Datatables module with the Editor plugin.

As far as getting the titles on each line, that’s actually pretty easy. See my comment here - Forms fields based on database content - #3 by logikonabstractions, and the pages linked from it. (Briefly, there’s a formset function, get_form_kwargs, that supplies the index of each form as it’s being built, allowing you to customize the form based upon the instance of the form.)

2 Likes

Thanks for the reply, Ken, appreciated !