Probably need to solve the problem individually:
First problem: Performance.
Have you looked at using select_related[1] in your query? That might
speed up the loop since it would do all the lookups in one big query.
(It'll need more memory since it is loading it all at once, but it
will probably be faster since the db doesn't have to do a query for
every forloop iteration.
Hmm, you might also look at just doing something like
Event.objects.values_list
('classes_involved__parent_disciplines__name', flat=True).distinct().
That works on some relations, but I haven't tested it in your case.
Second problem: Save doesn't work right.
Sorry, don't have any good ideas here. Maybe the relations aren't
being updated until you call the superclass's save method? (ManyToMany
fields are a separate table internally. It probably isn't updated
until you save.) That happens after you compute self.disciplines. You
could try doing a super(Event, self).save(*args, **kwargs) before you
compute the disciplines list, and then again after you have computed
it, to save the value. That is two saves into the db for one admin
save, but it shouldn't be too hard on your db.
new save method:
super(Event, self).save(*args, **kwargs)
# variable the will be saved in a charfield on the event.
disciplines = []
# get the related many to many field which has all the 'classes'
classes = self.classes_involved.all()
for this_class in classes:
parent_disciplines = this_class.parent_discipline.all()
for disc in parent_disciplines:
disciplines.append(str(disc.name))
self.disciplines = ', '.join(set(disciplines))
super(Event, self).save(*args, **kwargs)
Hope that helps,
Alex
[1] http://docs.djangoproject.com/en/dev/ref/models/querysets/#id4
On Jan 18, 10:32 am, Alastair Campbell <[email protected]> wrote:
> On Mon, Jan 18, 2010 at 2:59 PM, Alex Robbins
>
> <[email protected]> wrote:
> > Hmm, you posted the save method for your Event model, but not the
> > definition. I think you haven't gotten any feed back because no one
> > really knows what you are asking. Could you show the Event model, and
> > then show what a finished save should look like? What should the data
> > end up like?
>
> Thanks for the reply Alex, I hadn't posted the model definition
> because there's a lot of irrelevant stuff, but point taken, here's
> most of it:
>
> class Event(models.Model):
> name = models.CharField("Listings title", max_length=100)
> disciplines = models.CharField(max_length=250)
> classes_involved = models.ManyToManyField("categories.Class",
> blank=True, null=True)
> start_date = models.DateField("First day of the event")
> end_date = models.DateField("Last day of the event")
> approved = models.BooleanField()
> # some removed.
> def __unicode__(self):
> return self.name
>
> It's fairly simple, the complex bit is filling in the disciplines. I
> tried to add something to the save() to fill it in.
>
> In the Admin area, the event form shows the classes_involved (a choice
> of about 20), but doesn't show the disciplines.
> I was hoping to fill in the disciplines from the classes_involved.
> It's just used in listings, not for comparison, so a text string is
> fine.
>
> There are two problems:
> 1. The performance hit by going through 2 levels of manytomany fields.
> The first to get all classes_involved and then for each of those, to
> get it's parent. (I assume this is bad, my laptop seems to think so.)
> 2. When I edit an event, the save() doesn't seem to work from the
> form-data, it works on the previously saved data. I.e. if edit the
> classes_involved once and save, the discipline is not adjusted. If I
> save it twice, the discipline is adjusted.
>
> I was aiming for a cheap way to do two-level categorisation, however,
> I'm now thinking I just keep the categories separate and make admin
> users fill in the discipline manually.
>
> Kind regards,
>
> -Alastair
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.