Вопрос

This relates to a student electronic diary. The student page contains all the periods of study that day and each period can have one or more notes attached to the period.

Currently my code is :

notes = []
for note in db.StudentPeriodNote.objects.all():
    if student_id == note.student_id:
        if day == note.day:
            if period == note.period_text:
                notes.append(note)

Or it could be:-

notes = db.StudentPeriodNote.objects.filter(student_id=student_id, day=day, period=period)

At the end of a school year there could be 10-100,000 notes in the table. This query is called once for each period, so 10-12 times per day query or 5 times that in a week query.

My worry is that for each request by a student to display a day or weeks timetable, calling this query multiple times will be slow.

I could use index_together on the three fields which should speed up the search (note: if I add index_together to models.py then run dbsync, will the index be created or do I need to load the data again?)

I haven't used haystack, would this be an appropriate use for haystack?

Это было полезно?

Решение

I don't know if index_together or haystack is better for your problem but I can answer your question about adding index_together to your model.

For Django < 1.7, Django will not change the database after the initial syncdb. If you want to migrate models and data, you have to use South, a third party Django app.

http://south.aeracode.org/

The documentation on the website is pretty good and it's easy to use once you learn the basics. I use it in all of my Django projects to slowly morph the models and data as the requirements and features change.

For Django >= 1.7 (not released yet), the model and data migrations will be included in Django and you won't have to use South.

https://docs.djangoproject.com/en/1.7/topics/migrations/

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top