0

イベントテーブルがあります。参加者はスタッフと学生の2種類です。私がやりたいのは、出席者を別のテーブルに置くことです。

イベントテーブルには次のものが含まれます。

     created = models.DateTimeField(auto_now_add=True)
     modified = models.DateTimeField(auto_now=True)
     name = models.CharField(max_length=200)
     location = models.CharField(max_length=200)
     start_date = models.DateTimeField(auto_now=False, auto_now_add=False)
     about = models.TextField(null=True, blank=True)

     student_attendees = models.ManyToManyField(StudentProfile, null=True, blank=True)
     staff_attendees = models.ManyToManyField(StaffProfile, null=True, blank=True)

参加者テーブルには次のものがあります。

     event = models.ForeignKey(Event)
     content_type = models.ForeignKey(ContentType)
     object_id = models.PositiveIntegerField()
     profile = generic.GenericForeignKey('content_type', 'object_id')
     created = models.DateTimeField(auto_now_add=True)

イベントの学生とスタッフの出席者のデータを独自の参加者テーブルに転送または移行するにはどうすればよいですか。(私はSouthを使用していません。現時点では、Southを使用しないようにしたいと思います。)あなたの助けと提案をありがとう。

4

1 に答える 1

3

データをjsonにダンプし、手動で別のフィクスチャに分割してから、テーブルを変更(またはデータベースを再作成)してデータをロードするだけです。

または、スクリプトを記述して、いくつかのフィールドを手動で選択し、それらをピクルスにするか、jsonとして手動で、またはCSVとしてダンプします。

with open('students.csv', 'w') as st_file:
    writer = csv.writer(st_file)
    for a in Attendee.objects.filter(profile__status='student'):
        st_file.writerow(a.event_id, a.content_type, a.object_id, a.profile, ...)

次に、データをロードするための簡単なスクリプトを作成します。

with open('students.csv', 'w') as st_file:
    reader = csv.reader(st_file)
    for row in reader:
        event = Event.objects.get(id=row[0])
        event.student_attendees.add(Student.objects.get(row[2]))

(これは、object_idがStudentProfileテーブルの新しいIDと同一であることを意味します)

もちろん、これを行う前にデータベースをバックアップしてください。

実際には、より良い方法があります:

 python manage.py dumpdata app.event app.attendee --indent=4 > events.json

次に、新しいテーブルに合うように編集してから、

 python manage.py loaddata events.json
于 2013-02-07T21:05:05.343 に答える