On Tue, Oct 12, 2010 at 6:31 AM, Marc Aymerich <[email protected]> wrote:
> I'm writing an action for the django admin site. My model looks like:
>
> class order(models.Model):
> entity = models.ForeignKey(entity)
> [...]
>
>
> I select an arbitrary number of orders and I need to know the different
> entities that they have. It works on shell:
>
> >>> queryset = order.objects.all()
> >>> queryset.all().values_list('entity', flat=True).distinct()
> [1L]
>
> But on Admin seems the distinct part of the queryset doesn't work
>
> class orderAdmin(admin.ModelAdmin):
> [....]
> def invoice(self, request, queryset):
> print str(queryset.all().values_list('entity',
> flat=True).distinct())
> [....]
>
>
> Here the output of selecting all order elements(4), the output is a little
> different from the same operation in shell :(
>
> [1L, 1L, 1L, 1L]
>
>
> What's happening here?
>
Whenever distinct() doesn't seem to work like it should the first thing to
check is whether an oder_by() specified in the queryset is causing the
duplicates (see
http://docs.djangoproject.com/en/dev/ref/models/querysets/#distinct). Adding
order_by() to the queryset you are using will clear any previously specified
ordering and should eliminate the duplicates (if that is in fact what is
causing the problem).
Karen
--
http://tracey.org/kmt/
--
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.