Sylar427: perhaps start with the django docs on validation
rtm1046 joined the channel
Schrostfutz joined the channel
Sylar427
thanks shangxiao
Schrostfutz
Hi, I use the queryset.values() method to implement a group-by functionality. Values however additionaly changes the return value from object to dict-based return values. Is there a way to separate both?
webpigeon joined the channel
Sylar427 has quit
hylje
hmm?
shangxiao
hmm
Schrostfutz: that is the intended purpose of values()
Schrostfutz: why do you want to group by? are you doing aggregation?
ycon_ joined the channel
Sylar427 joined the channel
just playing around looks like values() doesn't effect a group by… perhaps you're doing something else to the queryset?
nix64bit has quit
Fish-Face
I am trying to use prefetch_related to prefetch a reverse relation through a foreign key like this: things = Thing.objects.filter(...).prefetch_related('foo__bar_set'), but even though I see the prefetch queries being performed, subsequent calls like things[0].foo.bar_set.get() result in further queries. What's weird is that things[0].foo.bar_set.all()[0] does not!
Schrostfutz
hylje, shangxiao: I reread the documentation, now it is more clear to me.
bakirelived has quit
bakirelived joined the channel
shangxiao
Fish-Face: seems expected to me, get() would be supplied with a different set of criteria right?
it looks like queryset.get() first does queryset.filter() but I can't tell whether that does anything with no args
shangxiao
emotionfolio: oh hang on i see what you're trying to do, i think misread your original question
emotionfolio: if you're after a subquery then you can do that with filter() btw
Nizumzen joined the channel
but basically you want to match a datacenter record per max in the aggregate right?
hylje
is django-webpack-loader good stuff? other takers for rigging up fancy frontends?
Sylar427 has left the channel
fub
I want to show a link in a template only if request.user.profile.role == 'admin'. Because I have to check this a few times, is there a convenient way to do this or should I write a custom template filter?
enkrypt has quit
jhfisc joined the channel
Fish-Face
fub: write a filter, annotate the request object, or include the property in the context
emotionfolio
shangxiao: sorry, i was afk.. Basicly yes, i need to get max free and group by datacenter
so i get most free server on datacenter
knbk
Fish-Face: pretty much all queryset methods will first create a clone of the queryset, which won't have any cached values. I think the only exceptions are .count() and exists(), which will use the cache if it's available
jhfisc has quit
stridebird1 joined the channel
fub
Fish-Face: what do you mean with "annotate the request object" ?
stridebird has quit
stridebird1 is now known as stridebird
blood_clot_drip joined the channel
blood_clot_drip
hello
can someone help me with an error?
Fish-Face
fub: I mean in your view, or in the middleware, you could say "request.admin = (request.user.profile.role == 'admin')"
fub
Fish-Face: and then {% if request.admin %}...{% endif %} in the template?
Fish-Face
yup
if you want to do that a lot then either do that in a middleware, or instead write a contextmanager to do something similar but add 'admin' to the context.
fub
Fish-Face: Is something like this also possible? {% if is_org_admin(user) %}...{% endif%} ?
Thats not a "template filter", more like a function, right?
zhalla_ has quit
Fish-Face
knbk: even .all() returns a clone, but it still works with prefetch_related...
hylje
fub: django templates don't support 'functions' like that, filters yes but they're kinda limited
Fish-Face
yeah you're getting out of the scope of template stuff
hylje
jinja can let you stick fancy logic in your templates if you like
fub
mh no, I want to stick with the default template language
so how would my filter look like without using a function? {% if user|is_org_admin %} ?
hylje
yeah
blood_clot_drip has left the channel
filters are simple one or two argument functions
fub
all right, then I will go for this. I think its the simplest solution :)
thanks
hylje
np
enkrypt joined the channel
code22 joined the channel
code22
?
exit
code22 has quit
hylje
:wq
knbk
Fish-Face: all() on a related manager does not create a clone if there are prefetched objects, but all() on a queryset does.
jhfisc joined the channel
Fish-Face
knbk: ahh gotcha. and presumably get() on the manager still does make a clone
knbk
Fish-Face: all methods other than .all() on a related manager are proxied to a queryset and will create a clone
Fish-Face
I see
shame the docs skip over that point! at least in the documentation on prefetch_related
jhfisc has quit
hylje
it's a bit nuts-and-bolts
sms has quit
Fish-Face
mm
stridebird has quit
jhfisc joined the channel
Troy1 joined the channel
jhfisc has quit
jhfisc joined the channel
zhalla_ joined the channel
emotionfolio
shangxiao: hello? :)
shangxiao
emotionfolio: hi!
what's up?
emotionfolio
shangxiao: we were talking about group by and max hehe
jhfisc has quit
lukebigg joined the channel
i dont have any ideas..
shangxiao
i believe you suggested using a subquery as well, trying filtering by subquery expression
there are a few sql tricks to get the datacenter record from a group by
distinct on is one of them iirc?
oh that's using distinct on with order by to get a max