django-datatable-view icon indicating copy to clipboard operation
django-datatable-view copied to clipboard

columns based on a queryset

Open mezitax opened this issue 5 years ago • 1 comments

How can I generate a table with an x number of columns based on a queryset? I explain:

class Assignment(models.Model):
	name = ...

class Student(models.Model):
	first_name = ...

class Calification(models.Model):
	student = models.ForeignKey('Student', related_name="calification_of_student")
	assignment = models.ForeignKey('Assignment', related_name="calification_of_assignment")

I need to see the list of students and for each assignment I want to see their qualification. The amount of assignment will be the number of columns and the cells should have the qualifications

mezitax avatar Feb 25 '20 00:02 mezitax

hello again, that time I resolved to create this table in html, and now after months I went back to this because I would like to have it in datatables.

What I want is that for each column, I get the grade corresponding to the instance and the assignment (column). image

class Califications_Of_Subject_List_Table(Datatable):
	number = columns.TextColumn("#", sources=[])
	last_name = columns.TextColumn(sources=["last_name"])
	first_name = columns.TextColumn(sources=["first_name"])
	def get_calification_value(self, instance, *args, **kwargs):
		"""return calification for each assignment for student, i need the assignment"""
		try:
			calification = Calification.objects.get(student=instance, assignment=???)
		except ObjectDoesNotExist:
			calification = None
		return calification
		
	def __init__(self, *args, **kwargs):
		""" create columns for each assignment published in subject"""
		super(Califications_Of_Subject_List_Table, self).__init__(*args, **kwargs)
		subject_slug = kwargs.pop('subject_slug', None)
		assignments = Assignment.objects.filter(subject__slug=subject_slug, status="publish").order_by("id")
		for assignment in assignments:
			self.columns[assignment] = columns.TextColumn(assignment.pk, sources=["assignments_of_student__califications_of_assignment__calification"], processor=self.get_calification_value)

How can I get the value of the assignment (column) to use in a filter inside the processor? I thought about sending the assignment as args but init doesn't know the instance yet At this point I am lost, someone to give me a hand?

mezitax avatar Jul 27 '20 17:07 mezitax