#django

/

      • renlo has quit
      • neilus has quit
      • ironfroggy_ joined the channel
      • hamdjan has quit
      • marp has quit
      • hamdjan joined the channel
      • ironfroggy has quit
      • onnudilol joined the channel
      • dtgq joined the channel
      • hutch34 joined the channel
      • dtgq has quit
      • lolidunno has quit
      • Debnet has quit
      • neredsenvy has quit
      • keimlink_ has quit
      • neilus joined the channel
      • hutch34 has quit
      • areca joined the channel
      • neilus has quit
      • hamdjan has quit
      • areca has quit
      • hamdjan joined the channel
      • bwreilly has quit
      • dtgq joined the channel
      • rouentry joined the channel
      • Genitrust has quit
      • Genitrust joined the channel
      • dtgq
        When using Class Based Views, is a new object created for every request?
      • jfisk87 has quit
      • jessamynsmith
        dtgq: you mean a new instance of the class?
      • dtgq
        Yes
      • jessamynsmith
        yes, web requests are individual
      • that doesn't really have anything to do with class-based views
      • dtgq
        In that case, should methods that don't use "self." be decorated with @staticmethod?
      • eka joined the channel
      • jessamynsmith
        you don't have to but it'll cut down on warnings
      • eka has quit
      • dtgq
        I haven't encountered any warnings yet, what would case the warnings? I was thinking more about saving memory...
      • maryokhin has quit
      • what would cause* the warnings
      • TheMetaTroll joined the channel
      • jessamynsmith
        a linter
      • TheMetaTroll has left the channel
      • dtgq
        Alright, but I haven't seen anyone decorate their staticmethods, why?
      • FreedomFighter joined the channel
      • mattmcc
        There isn't usually a use for them in CBVs.
      • jessamynsmith
        yea, what mattmcc said
      • natea joined the channel
      • dtgq
        I don't follow... for example if I have a method that doesn't use `self` like this:
      • def get(request, pk): return JsonResponse(Profile.objects.get(pk=pk).values())
      • Should that use @staticmethod?
      • jessamynsmith
        why doesn't your get method take self?
      • hamub has quit
      • tdy has quit
      • dtgq
        I have it using @staticmethod in my code already since it doesn't use self
      • jessamynsmith
        noooo don't do that
      • dtgq
        Is this bad practice?
      • jessamynsmith
        you should maintain the method signatures from the parent class
      • dtgq
        Why, what could go wrong with this? (honest question)
      • jessamynsmith
        give me a minute, I'm putting together some code
      • dtgq
        ok
      • jonmoshier
        hey djangonauts, is there a good way to make models 'flexible'? I'm writing a small CMS and i'd like to be able to alter the content of a Post without just dumping everything into a content field?
      • jessamynsmith
        dtgq: so, first thing, if you lint your code it will complain that your method signature doesn't match the parent class
      • dtgq
        Ah ok, but it would still work?
      • thinkt4nk joined the channel
      • mattmcc
        dtgq: It doesn't make any sense. You could just as easily write a function view, 'def profile_json(request): return JsonResponse(...)'
      • flobin has quit
      • dtgq
        But I'm using Django Rest Framework, their @api_view is just a sneaky trick to transform my function view into a CBV...
      • tdy joined the channel
      • jessamynsmith
        jonmoshier: I don't really understand your question
      • dtgq
        But yes, if I decide that I don't need APIView for something, I will write a straight-up Django function view
      • Trying to understand why people don't use @staticmethod for their static methods, the way I see it, it's just wasting resources on duplicated methods for every request
      • jessamynsmith
        what?
      • Genitrust has quit
      • neilus joined the channel
      • the class is going to be instantiated once per request regardless
      • jonmoshier
        jessamynsmith: Fair, I don't think I explained it well. I'll give you a use case: When I create a new Post, I want to add-in different 'assets' depending on the post. I might want to add an image, I might want to add a javascript file, I might want to add, assets that I haven't even thought of yet.
      • Genitrust joined the channel
      • jessamynsmith: But I don't want to dump all of that into a rich text editor and have it be lost in the ether.
      • mattmcc
        dtgq: A CBV is recreated for each request to guarantee that no information leaks across.
      • dtgq
        Yes the class would be, but also each method inside. If @staticmethod is used, they can be reused for every instance
      • mattmcc
        That's why making things static methods doesn't serve any purpose.
      • dtgq
        If self isn't used anywhere, then there shouldn't be anything to leak?
      • mattmcc
        Self is used all over the place in CBVs..
      • dtgq
        I mean anywhere inside the method that I'm decorating with @staticmethod
      • jessamynsmith
        I mean, I guess you could write your own class, not inheriting from CBVs, and you could write your own webserver, that doesn't isolate requests
      • but... why?
      • mattmcc
        Yeah, I'm still not seeing a point.
      • jessamynsmith
        depending on the webserver used, the requests might be in different threads altogether
      • or different processes
      • dtgq
        well because these methods don't need to be duplicated for each request
      • jessamynsmith
        jonmoshier: I think normally you'd handle this with some concept of attaching items
      • you might want to look at wagtail to see how they modelled it
      • or some other cms
      • ironfroggy_ has quit
      • jonmoshier
        Yeah, I have wagtails code open in github.
      • dtgq
        Perhaps I have a misconception, so I want to confirm something: With every request, each variable and method inside the View class is cloned in memory, correct?
      • Genitrust has quit
      • jessamynsmith
        I don't know what you mean by cloned in memory
      • cloned from what?
      • sandino joined the channel
      • sandino has quit
      • sandino joined the channel
      • dtgq
        For example if this is at the top of my CBV:
      • jessamynsmith
        in any case, it is a bad practice to make the inheriting class have the same method names with different signatures
      • (as the parent class)
      • it's unnecessary obfuscation
      • dtgq
        class ProfileList(APIView):
      • foo='bar'
      • jessamynsmith
        it's true that due to how python works, the parent class method will be overridden
      • dtgq
        now, every time a request comes in, a new 'bar' will be cloned in memory with each View instance, right?
      • so if the View has 10 requests, now ten 'bar' strings have gone into memory?
      • jessamynsmith
        I think you might want to read up on how webservers handle requests
      • dtgq
        webserver as in gunicorn?
      • jonmoshier
        dtgq: Are you worried about and therefore trying to optimize your performance?
      • jessamynsmith
      • royendgel has quit
      • royendgel joined the channel
      • dtgq
        Alright so if I understand this correctly, there are 4 to 12 workers and each worker is only working on one request at a time... So it's all a wash if @staticmethod is used. Is that correct?
      • neilus has quit
      • jessamynsmith
        that's my understanding
      • you aren't buying yourself anything
      • dtgq
        Ah ok. So just to confirm with an analogy: It's like buying upgrades for a rental car. Is that right?
      • jessamynsmith
        objects are cheap in python
      • roughly, in the sense that you give it back and get another anyway
      • dtgq
        Ok, this makes perfect sense to me now. My inner premature-optimization-hamster has been satisfied
      • Thanks for your explanation, really appreciate it :)
      • jessamynsmith
        heh
      • I'm a pretty big fan of writing for comprehensibility first, profiling, then optimizing
      • sandino has quit
      • if you try to optimize before you've profiled, there is a pretty good chance you'll optimize the wrong things
      • dtgq
        Yeah I've read that a lot, but I kept coming back to this, it was nagging at the back of my mind that it could be refactored or optimized etc
      • DrNo
        hey there :)
      • dtgq
        With more experience I think I'll grow to that procedure you have
      • olrrai joined the channel
      • DrNo
        I'm essentially trying to serve a static site, whilst including all the bits and pieces i need within my prebuilt html/css/js
      • Mark_2 joined the channel
      • my intention is to then chop and change with some python via templates
      • is this a reasonable workflow or am i going top down?
      • bambanx joined the channel
      • SecondForm_Glenn joined the channel
      • jessamynsmith
        DrNo: you're using django just for static?
      • dtgq has quit
      • SecondForm_Glenn has quit
      • ironfroggy joined the channel
      • buff3r has quit
      • SecondForm_Glenn joined the channel
      • SecondForm_Glenn has quit
      • dtgq joined the channel
      • dtgq has quit