0

学生とスタッフの 2 種類の出席者を保持する Event テーブルがあります。学生の列(多対多)にいる人を取得して、自分のテーブルに配置するにはどうすればよいですか?

class Event(models.Model):
    ...
    students = models.ManyToManyField(StudentProfile, null=True, blank=True)
    staff = models.ManyToManyField(StaffProfile, null=True, blank=True)
    ...

新しいテーブル: ?

class StudentAttendees(models.Model):
    profile = models.ManyToManyField(StudentProfile, through='Event')
    event = models.ForeignKey(Event)
    created = models.DateTimeField(auto_now_add=True)

戻り値:

django.core.management.base.CommandError: One or more models did not validate:
profiles.studentattendees: 'profile' is a manually-defined m2m relation through model Event, which does not have foreign keys to StudentProfile and StudentAttendees

事前にご協力いただきありがとうございます。

4

1 に答える 1

0

このようなものがうまくいくかもしれません:

for some_event in Event.objects.all():
    student_attendees_object = StudentAttendees.objects.create(event=some_event)
    for student in some_event.students_set.all():
        student_attendees_objects.profile.add(student)

何を達成しようとしているのかわかりませんが、学生の出席者に関する追加情報が必要な場合は、throughパラメーターを使用して中間テーブルを調べてください。これに関するドキュメントを確認してください。

于 2013-02-11T18:12:46.557 に答える