#django

/

      • Sylar427
        can you give me some guideliness what to look for
      • shangxiao
        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?
      • emotionfolio
        shangxiao: how can filter() help me out here? :)
      • camouflage has quit
      • i cant find anything useful in docs
      • fub
        Hi. I followed this middleware to make a login required for all pages: http://onecreativeblog.com/post/59051248/django...
      • hylje
        .get() always resolves into a new query
      • fub
        I've put this middleware in myproject/myapp/middleware/login_required_middleware.py and put this in my settings.py MIDDLEWARE:
      • shangxiao
      • csotelo joined the channel
      • Fish-Face
        shangxiao: I am not sure how get() works under the hood but I don't see that it's changing the query in the docs
      • shangxiao
        emotionfolio: if you put the new max in an F() perhaps that'll work?
      • fub
        'myproject.middleware.login_required_middleware.LoginRequiredMiddleware'
      • Now I get this error when starting the dev server:
      • FunkyBob
        fub: which version of Django are you using?
      • fub
        TypeError: object() takes no parameters
      • TypeError: object() takes no parameters
      • sorry for the duplicate
      • FunkyBob: one second
      • 1.11.1
      • FunkyBob
        ok
      • that's an old article, using the old interface for middleware
      • and ytou've put it in the list of new interface middleware
      • one tick
      • fub
        oh, okay
      • so I need to find a different middleware snippet?
      • FunkyBob
      • just need to update it :)
      • twowheels joined the channel
      • emotionfolio
        shangxiao: .filter(F(Max('free'))) u mean this?
      • shangxiao
        no, that's not what the doc link suggests :P
      • phinxy joined the channel
      • emotionfolio
        didnt see the link :)
      • fub
        FunkyBob: works, awesome. thanks!
      • emotionfolio
        shangxiao:
      • .filter(free=F('free'))?
      • nah, that will not do the work
      • shangxiao
        =/
      • you're not even trying now
      • (you haven't used max there)
      • jhfisc joined the channel
      • emotionfolio
        i cant get how this orm works..
      • it is so simple queryset in raw sql
      • adsworth_ joined the channel
      • Troy1 joined the channel
      • adsworth has quit
      • shangxiao
        try what you just pasted but put a max, you may need add it to the annotate() as an extra column
      • emotionfolio
      • i have tried this way
      • it tells that free is an aggregate
      • twowheels has quit
      • webpigeon has quit
      • jhfisc has quit
      • shangxiao
        ok let me play around
      • Fish-Face
        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
      • looks like distinct() does DISTINCT ON with postgres too: https://docs.djangoproject.com/en/1.11/ref/mode...
      • x4ec7 has quit
      • adsworth_ has quit