1

I have a model named ClassRoom,

class ClassRoom(model.Model):
    class_name = models.CharField(max_lenth=100)
    students = models.ManyToManyField(User)

and view.py like this.

def addstudents(request):
    class_room = ClassRoom.objects.get(id=1)
    class_name.students.add(request.user)

I added the students to ClassRoom like this. I wanted to list all the students in class, who joined today. How can I filter this students by their joining time?.

When we using django ManyToManyField, then django create an extra table in database. In that table contains created time. Can I filter using this field?

Savad KP
  • 1,625
  • 3
  • 28
  • 40
  • Define "joined today", how do you keep track of the day they joined?.. – Sayse Mar 08 '16 at 14:48
  • That is my problem. In database django created an extra table for this many2many. in that table contains created date. Can we filter using that table created field value? – Savad KP Mar 08 '16 at 14:50
  • Are you sure there is a `created` field? If it is, you can use it to filter – Gocht Mar 08 '16 at 15:03
  • 1
    See the docs on [extra fields on many-to-many relationships](https://docs.djangoproject.com/en/1.9/topics/db/models/#extra-fields-on-many-to-many-relationships). – Alasdair Mar 08 '16 at 15:05
  • So you could be able to use `class_room.students.filter(created=datetime.today())`, where `class_room` is a `ClassRoom` instance – Gocht Mar 08 '16 at 15:10
  • @Gocht When I tried this. Then I got an error like this `Cannot resolve keyword 'created' into field. Choices are:.....` – Savad KP Mar 08 '16 at 15:12
  • So it's not a field. – Gocht Mar 08 '16 at 15:13
  • @Gocht but when I add a students to `ClassRoom`, django saved the time to `appname_classroom_students` table. There is no option for filtering using this time? :( – Savad KP Mar 08 '16 at 15:16
  • You haven't included any details about what `User` is, if its django's auth model then there isn't a `created` field. You should update your question to include the relevant information. – Sayse Mar 08 '16 at 15:17
  • @Sayse the user is already created one. Now that user is added to a ClassRoom. User created time and user added to a ClassRoom time, both are different. – Savad KP Mar 08 '16 at 15:20
  • Again, the django auth [User](http://stackoverflow.com/questions/35483580/passing-a-list-through-url-in-django/35483756#comment58666329_35483756) model does **not** have a `created` field, there is a `date_joined` which is about the closest thing. – Sayse Mar 08 '16 at 15:22

1 Answers1

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()
Gocht
  • 9,924
  • 3
  • 42
  • 81
  • The user is already created one. Now that user is added to a ClassRoom. User created time and user added to a ClassRoom time, both are different. – Savad KP Mar 08 '16 at 15:21
  • The User created time is not here, `date_joined` will take the time when you create a `ClassMate` instance with `ClassRoom` and `User` instances – Gocht Mar 08 '16 at 15:23