Re: How to concatenate a list of Q objects?

2014-05-29 Thread Степан Дибров
from django.db.models import Q
import operator

qs = SomeModel.objects.all()

# use reduce
rules = []
for rule_key, rule_value in calculated_rules:
rules.append(Q(rule_key=rule_value))

if rules:
qs = qs.filter(reduce(operator.or_, rules))

# not use it
q_collect = None
for rule_key, rule_value in calculated_rules:
new_q = Q(rule_key=rule_value)
if q_collect is None:
q_collect = new_q
else:
q_collect = q_collect | new_q

if rules:
qs = qs.filter(q_collect)

вторник, 6 апреля 2010 г., 9:10:08 UTC+4 пользователь Daniel написал:
>
> Hi, I think that this must be super easy, but I'm kind of stumped.
>
> I have a list qObjects = [qObject1, qObject2, qObject3]
> What I'd like is to form this query:  Sample.objects.filter(qObject1,
> qObject2, qObject3)
>
> How would I accomplish what I need?  Thanks!
>
>

-- 
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/80a976f7-6591-4ed8-9cb8-399f986d41df%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Model field's verbose_name from database?

2014-05-28 Thread Степан Дибров
from django.utils.functional import lazy

def field_verbose_db(field_name):
from app.models import FieldNamesMap
return FieldNamesMap.objects.get(key=field_name).title

field_verbose_db_lazy = lazy(field_name, unicode)

first_name = models.CharField(..., 
verbose_name=field_verbose_db_lazy('first_name')) 

вторник, 4 февраля 2014 г., 22:03:08 UTC+4 пользователь Jon Dufresne 
написал:
>
> Hi, 
>
> I have a model with fields that allows the user to modify the 
> user-facing names of these fields. Think "first name", "last name", 
> "email", the user may prefer these be displayed as "given name", 
> "surname", "email address". This is configured exactly once throughout 
> the system. So in this example *all* instances of "first name" should 
> instead be displayed as "given name". Is it possible have a model 
> where "verbose_name" is not a static string, but instead comes from 
> the database? Perhaps with some lazy loading trickery? I want to do 
> something in the spirit of: 
>
> first_name = models.CharField(..., verbose_name=lazy_load_name_from_db()) 
>
> My hope is to eliminate the need to think about what to call this 
> field when displayed to the user. 
>
> Thanks, 
> Jon 
>

-- 
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/18b0c5bc-71c3-42d7-841e-3afd13c0bd71%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.