Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

columns based on a queryset #239

Open
mezitax opened this issue Feb 25, 2020 · 1 comment
Open

columns based on a queryset #239

mezitax opened this issue Feb 25, 2020 · 1 comment

Comments

@mezitax
Copy link

mezitax commented Feb 25, 2020

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
Copy link
Author

mezitax commented Jul 27, 2020

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant