2👍
You can add extra fields to a M2M relationship table by using a custom table (docs here).
class ClassRoom(models.Model):
class_name = models.CharField(max_length=100)
students = models.ManyToManyField(User, through='ClassMate')
class ClassMate(models.Model):
class_room = models.ForeignKey(ClassRoom, on_delete=models.CASCADE)
user = model.ForeignKey(User, on_delete=models.CASCADE)
date_joined = models.DateField(auto_now_add=True)
Then you can do this:
class_room = ClassRoom.objects.get(...)
class_room.students.filter(date_joined=datetime.today())
Keep in mind that (as docs says) now you have to instance ClassMate
objects to add a relationship:
class_room = ClassRoom.objects.get(...)
user = request.user
class_mate = ClassMate(class_room=class_room, user=user)
class_mate.save()
Source:stackexchange.com