Wednesday, October 13, 2010

Re: distinct doesn't work on admin actions¿?



On Wed, Oct 13, 2010 at 3:57 AM, Karen Tracey <kmtracey@gmail.com> wrote:
On Tue, Oct 12, 2010 at 6:31 AM, Marc Aymerich <glicerinu@gmail.com> 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).


Thanks you very much Karen, the problem was this! Now it works as expected :)   
 
-- 
Marc

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment