Hi everyone,
I have something that works, but I'm guessing it's *very* inefficient,
I'm guessing that nested for-loops for two layers of many to many
fields is bad, but how could I go about this better?
The aim is for my admin users to fill in the sub-categories (classes),
and for the system to work out which parent categories (disciplines)
are involved.
I have a separate model for taxonomy, and within my 'categories'
models.py I have:
class Discipline(models.Model):
name = models.CharField(max_length=50)
slug = models.SlugField(unique=True)
class Class(models.Model):
name = models.CharField(max_length=50)
slug = models.SlugField(unique=True)
parent_discipline = models.ManyToManyField("Discipline")
Sorry about using 'Class' as the model name, but unfortunately it's
the only thing that makes sense for this categorisation.
(E.g. the discipline might be slalom, which has a class of amatuer within it.)
This is added to the save method for my Events model:
# 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))
this_set = set(disciplines)
this_list = list(this_set)
self.disciplines = ', '.join(this_list)
super(Event, self).save(*args, **kwargs)
Any ideas on how I could go about this better? I'd hoped to make
things easier with a centrally defined taxonomy, but so far it isn't
worked to well.
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.