Re: How to annotate many values in one queryset

2014-08-18 Thread 9devmail


> As an aside, this can probably be written as: 
>
>   current_user_vote = item.vote_set.filter(user=request.user) 
>
> > 
> > I tried to use annotate() two times, however it filtered out everything 
> > except items current user voted on. 
>
> Just use it once. 
>

Thank you for answer, however I still can't see how can I do it.

Using annotate only once won't let me use filter() to exclude users 
different than the current one.

Item.objects.annotate(
score=Sum('votes__value'),
current_user_vote=Sum('votes__value')
)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/38b52a30-b731-4eb6-b36c-550911638a87%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


How to annotate many values in one queryset

2014-08-18 Thread 9devmail
Is it possible to annotate two different values to one QuerySet?

Example queryset:

Item.objects.annotate(score=Count('votes'))

I would like every item in the above QuerySet to contain the following 
field:

current_user_vote = Vote.objects.filter(user=request.user).filter(item=item)

I tried to use annotate() two times, however it filtered out everything 
except items current user voted on.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8f63fafa-d224-4b25-875e-5d2c73c2e0f8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Efficient way to perform many queries

2014-08-17 Thread 9devmail
Thank you, it works perfectly :)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/91e4565b-0e58-4060-9a56-d973e8d55ef6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Efficient way to perform many queries

2014-08-16 Thread 9devmail
prefetch_related() works great.

How about prefetching some computed value instead of records?

{% for item in items %}

{{ item.name }}

{{ item.score }}

{% endfor %}

I want to prefetch 'score' for every item, where 'score' would be 
equivalent to:

score = Votes.objects.all().count() - Votes.objects.filter(value=False).
count()

Is there any way to prefetch such query?

Code below prefetches all data for Vote records. How can it be changed to 
prefetch only the score?

Item.objects.all().prefetch_related('votes')



-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c63c09b7-0d07-464a-be96-35fd544b7545%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Efficient way to perform many queries

2014-08-15 Thread 9devmail
Thank you.

What if I'm using GenericForeignKey in Tags model? Can I still use it?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b39f083e-27bf-4059-ab4b-5700615fc93d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Efficient way to perform many queries

2014-08-15 Thread 9devmail
I am writing application similar to django-taggit 
 and I was wondering about its 
performance.

Example for list of items:

{% for item in items %}

{{ item.name }}

{% for tag in item.tags.all %}
{{ tag }}
{% endfor %}

{% endfor %}

Above code creates massive amount of separated SQL queries as can be 
observed in Django Toolbar (click on image to enlarge).




Is there any way to make it more efficient? I was thinking about merging 
these queries into one big. Thanks in advance for any tips.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/25563cc0-ad0f-45c7-9d96-0326d01b869e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to decompose URL into model and parameters

2014-08-14 Thread 9devmail
Yes, I figured it out. Thank you.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/352776cd-f859-47c9-91c5-0b93f7620404%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to decompose URL into model and parameters

2014-08-14 Thread 9devmail
Line

match = urlresolvers.resolve(urlsplit(url).path)

stops executing the script and redirects me to given URL.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/606cc513-9e65-4740-96f7-7aa3dbb664ad%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


How to decompose URL into model and parameters

2014-08-14 Thread 9devmail
I want to parse an application URL just like Django does.

url = 'http://www.example.com/path/to/myview/123'
view, params = decompose(url)
# now view="MyView", params=('123',)

How can it be done?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/08a4ed71-ec57-4d30-9223-ba6dbb7c2542%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


How to sort by many custom methods in Django Admin

2014-07-10 Thread 9devmail
 

I want to be able to sort by several custom methods in Django Admin. This 
question on StackOverflow 

 
provides solution for one method only.

I tried to modify it:

from django.db import models
class CustomerAdmin(admin.ModelAdmin):
list_display = ('number_of_orders','number_of_somevalue') # added field

def queryset(self, request):
qs = super(CustomerAdmin, self).queryset(request)
qs = qs.annotate(models.Count('order'))
qs = qs.annotate(models.Count('somevalue')) # added line
return qs

def number_of_orders(self, obj):
return obj.order__count
number_of_orders.admin_order_field = 'order__count'

def number_of_somevalue(self, obj): # added method
return obj.somevalue__count
number_of_somevalue.admin_order_field = 'somevalue__count'

and it works incorrectly. It seems that it multiplies the count values 
instead of counting them separately.

Example:

I have 2 orders and 2 somevalues, but in the panel I see 4 orders and 4 
somevalues. Adding another method with yet another value makes it 8 (2*2*2).

How can I fix it?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/4737bbba-8c4b-47b8-bcd8-eb69b71f5497%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to filter objects by values of foreign objects in Django admin

2014-07-07 Thread 9devmail
No, it does not. I cannot pass any parameters to this function, because it 
is being invoked automatically by the admin.

class MyAdmin(admin.ModelAdmin):
list_display = ['num_of_trans']

How can I pass any arguments to it?

Please, read my question carefully.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/5748b53a-d697-40df-a732-4a6d14d742ab%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to filter objects by values of foreign objects in Django admin

2014-07-07 Thread 9devmail
Thank you, but I think you misunderstood.

I do not want to filter Product objects. I want to filter Transaction 
objects.

Please, take a look at provided example.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/d5ba360d-2838-48f3-9cd5-0baa8111be1b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to filter objects by values of foreign objects in Django admin

2014-07-07 Thread 9devmail
Thanks for pointing it out.

However, my question is about a different issue.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/fa5bbf2c-c699-4ef2-8b4e-85b14206d7f6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


How to filter objects by values of foreign objects in Django admin

2014-07-07 Thread 9devmail


There are tables Product and Transaction. In Product admin there is a field 
that shows how many transactions have been created with this product.

Now, I want to filter the period of time in which these transactions were 
created. For example, there was 30 transactions in the last week, but 100 
in the last month.

class Transaction(models.Model):
created = models.DateTimeField()
product = models.ForeignKey('Product')
class Product(models.Model):
name = models.CharField()

def num_of_trans(self):
return 
Transaction.objects.filter(created="""DATE_VALUE_HERE""").filter(product=self.id).count()

I tried to adjust the queryset in a class that extends SimpleListFilter, 
however I did not manage to make it work.

Another thing I thought of was to add an additional parameter to 
num_of_trans function, but it seems there is not way to pass this parameter 
while using the filter.

I would appreciate any tips.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c1612d89-d28b-4c82-b17d-703c43cc581d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


How to create a custom admin panel for many models

2014-07-04 Thread 9devmail
 

I want to create a custom ModelAdmin, but for a mix of models, not just one.

I have two example models:

class Article(models.Model):
author = models.GenericIPAddressField()
# ...
class Comment(models.Model):
author = models.GenericIPAddressField()
# ...

Now I want to be able to have a ModelAdmin with fields:

author
number_of_comments
number_of_articles

How can I achieve it?

I created a new ModelAdmin with overridden queryset method, but I was not 
able to register it, because Article and Comment are already registered.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/0a2d344f-755d-4108-83bd-2c672cce4e30%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


How to use CreateView with crispy-forms

2014-05-28 Thread 9devmail
 

I want to use CreateView along with crispy-forms generated layout. However, 
it seems that everything I pass to *self.helper* in *forms.py* is ignored. 
Form renders nicely, but only with fields generated by CreateView - all 
fields passed to *Layout* are missing.

My views.py

> class MyView(CreateView):
> form_class = MyForm
> model = MyModel
>
> def form_valid(self, form):
> pass
>
> My forms.py:

> from django.forms import ModelForm, Textareafrom crispy_forms.helper import 
> FormHelperfrom crispy_forms.layout import Submit, Layoutfrom 
> crispy_forms.bootstrap import FormActions
> from .models import MyModel
> class MyForm(ModelForm): 
>
> def __init__(self, *args, **kwargs):
> super(MyForm, self).__init__(*args, **kwargs)
> self.helper = FormHelper(self)
> self.helper.form_method = 'POST'
> self.helper.add_input(Submit('submit', 'Submit'))
> self.helper.layout = Layout( 
> FormActions(Submit('BlahBlah', 'BlahBlah', 
> css_class='btn-primary')))
>
> class Meta:
> model = MyModel
> fields = ['xxx', 'yyy']
>
> My image_form.html:

> {% load crispy_forms_tags %}... enctype="multipart/form-data">
> {% csrf_token %}
> {{ form|crispy }}
>
> What else should I do to make it work?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/2555778c-3f3f-43b4-8670-80ea8abdd1ba%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.