вторник, 15 января 2013 г., 16:34:36 UTC+4 пользователь Amirouche написал: > > > The example in the code is not working, <<My>> class is not referenced > anywhere. I'm not familiar with multi-widgets but if a tree widgets is not > what you want or don't get it work, I think it's the right approach. >
Unfortunately, I don't know how it should be. Also I believe your idea of trees is much simpler and cleaner and thus much better. I've installed django-mptt, and it works like a charm. But I'm worried it would be difficult to upgrade to a new Django version if I use third-party applications. So I was trying to implement similar approach using ForeignKey to self, just like django-mptt does: class Tag(models.Model): name = models.CharField(max_length=50, unique=True, db_index=True) parent = models.ForeignKey('self', null=True, blank=True, related_name='children') def __unicode__(self): if self.parent: return '%s%s' % ('-', self.name) return self.name class AlbumAdminForm(forms.ModelForm): class Meta: model = Album tags = forms.ModelMultipleChoiceField(queryset=getqueryset(), required=False, widget=FilteredSelectMultiple('tags', False )) getqueryset() should return a QuerySet Object, sorted like this: parent1 child1_of_parent1 child2_of_parent1 parent2 child1_of_parent2 child2_of_parent2 but I couldn't solve this yet. Concatenating of QuereSets with | operation drove me nowhere (result of | doesn't respect an order of concatenation) and QuerySet.annotate() didn't help either. Also no luck with sorted(): it returns some iterable object which is not a QuerySet, and Django fails while rendering a template. It's quite simple to get id list sorted in the right order: def getalltags(): ids = [] cats = Tag.objects.filter(parent__isnull=True).order_by('name') for id in cats.values_list('id', flat=True): ids += [id] tags = Tag.objects.filter(parent=id).order_by('name') ids += tags.values_list('id', flat=True) But I couldn't get a QuerySet sorted this way yet. So I still keep thinking. Thank you again for all your help. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/9Hl2lisD_qMJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.