#django

/

      • MarkusH
        other than that, no idea
      • fesener_
        Hi everyone, I'm using a plug-in named "django-countries" , anyone familiar with it? I'm having problem with sorting the objects by their name, it always orders them by their country code
      • rmwdeveloper
        +Markush: thanks
      • MarkusH
        but not sure if partials work with migrations
      • Haven't tried that one
      • fesener_
        Country.objects.order_by('country__name') is not working because it's actually a "CountryField()" rather than a foreignkey
      • Goopyo joined the channel
      • ruind has quit
      • andrew9184 joined the channel
      • jessamynsmith
        Does Country really have a member country? Or just a member name?
      • MarkusH
        fesener_: I don't see a way to order on database level
      • fesener_
        jessamynsmith, sorry i didnt understand the question
      • MarkusH, thanks for checking it out anyway :)
      • something like "return unicode(self.country.name)" works perfectly but i cant do country__name for ordering
      • MarkusH
        You might want to watch out for SmileyChris in here ;)
      • fesener_
        yea I was hoping to find him here :)
      • MarkusH
        fesener_: django-countries doesn't save the name in the DB
      • it is hard coded in python code
      • fesener_
        ah thats why I cant get __name
      • i wish there was a way
      • MarkusH
      • fesener_
        yea :( i guess i cant order them by name, thank you
      • MarkusH
      • but depending on how may objects you need to sort, I'd just do that on python level
      • fesener_
        it will be like 30-40 countries at most
      • MarkusH
        sorted(some_list, key=operator.attrgetter('someattr'))
      • fesener_
        can you help me adapt that to my Country.objects.order_by() line?
      • MarkusH
        countries = Country.objects.all() ; sorted(countries, key=operator.attrgetter('name')) ?
      • fesener_
        let me try
      • it says operator is not defined
      • MarkusH
        import operator
      • fesener_
        'Country' object has no attribute 'name'
      • MarkusH
        what is "Country"?
      • fesener_
        countries = Country.objects.all()
      • lefteris has quit
      • MarkusH
        Let's see your Country model, please
      • codeme has quit
      • That's better
      • fesener_
      • i guess this is what you are asking?
      • autrilla__
        Is "for temperature in self.temperature_set.filter(date__gte=start).filter(date__lte=end):" more efficient than .all() and an if afterwards?
      • MarkusH
        fesener_: ok, make it attrgetter('country.name')
      • dray3 has quit
      • autrilla__: depends
      • fesener_
        MarkusH, making it "countries = sorted(countries, key=operator.attrgetter('country.name'))" made it work! thank you so much
      • autrilla__
        MarkusH, mind elaborating?>
      • fesener_
        you are awesome
      • scofmb_ has quit
      • MarkusH
        fesener_: thanks ^^, you're welcome
      • scofmb joined the channel
      • autrilla__: no :-P
      • autrilla__
        No you don't mind or no you won't elaborate? :p
      • MarkusH
        autrilla__: well, it probably depends on the amount of data you are querying
      • autrilla__
        Well not much now, but i production I can expect every user to have thousands of the temperature model
      • MarkusH
        but I guess the .filter() thing is mostly faster ;)
      • autrilla__
        in* production
      • The .filter runs on the database right?
      • MarkusH
        yes
      • autrilla__
        Which is probably faster as it's probably C instead of python
      • MarkusH
        so you don't have to construct billions of objects
      • yes
      • autrilla__
        Great, thanks
      • Sinestro joined the channel
      • tWoolie
        also, because it uses the database indexes which are pre-sorted
      • so make sure you put an index on the name attribute if you're going to be sorting on it
      • autrilla__
        Can I show related stuff to an object on the admin?
      • crakrjak joined the channel
      • fesener_ has quit
      • jessamynsmith has quit
      • I get __str__ returned non-string (type NoneType)
      • mattmcc
        Probably because Zone.__str__ doesn't always return a string.
      • Incidentally, you may be jumping into using multi-table model inheritance prematurely.
      • autrilla__
        that's how I'd do it in java
      • mattmcc
        Well, these classes map to database tables, they're not like ordinary classes.
      • So you should be thinking about the DB structure more than what makes sense in Java.
      • jessamyn_ joined the channel
      • autrilla__
        Well, so the Zone class shouldn't me a model?
      • Genitrust joined the channel
      • MarkusH
        autrilla__: mattmcc: I'd go with something like https://dpaste.de/Nt2Q
      • mattmcc
        Depends on what other types of zones you have in mind, and how far they nest.
      • SQL DBs _suck_ at nesting if you don't use the right structure.
      • Siecje has left the channel
      • autrilla__
        mattmcc, that's basically it... the original plan was one Farm with as many subzones as you want, but just one depth level
      • Well, not one Farm, many farms each with many Subzones
      • mattmcc
        Yeah, then you want models to relate to each other, don't need inheritance to do that.
      • hillaj has quit
      • schinckel
        You may also want __unicode__, rather than __str__.
      • MarkusH
        You can use an abstract model for common fields
      • schinckel: no
      • autrilla__
        mattmcc, but then Devices can be on either Farms or Subzones
      • mattmcc
        This is one of those fundamental disconnects between OOP and SQL.
      • MarkusH
        you want python 3
      • schinckel: use __str__ and @python_2_unicode_compatible
      • autrilla__
        That's why I had a superclass, so Devices could have a Zone
      • schinckel
        Wonder what I'm doing with my alleged 2-and-3 compatible apps then... ;)
      • autrilla__
        MarkusH, so with your suggestion, everything is a Zone, and if the farm attribute is not set, it's a farm, and if it is set, it's a subzone?
      • MarkusH
        autrilla__: yes
      • autrilla__
        I could probably name it parent instead of farm and let the user decide how much depth he wants
      • MarkusH
        autrilla__: if you use "my" pattern, you don't want more than 1 level of nesting
      • 2 at most if you have a medium sized database
      • autrilla__
        why not/
      • ?*
      • MarkusH
        this pattern sucks for relational databases
      • mattmcc
        When you want to nest more, you want a tree pattern like materialized-path or nested-set.
      • Which add extra fields to your models that allow for tree traversal without having to use recursion.
      • MarkusH
        or, if you want to have arbirary nesting, look at django-treebeard or others (never used that one myself)
      • Genitrus_ joined the channel
      • mattmcc
        Yeah, treebeard provides models for both patterns.
      • autrilla__
        Well, in my case each query wouldn't have to handle more than 500 rows at a time, I think all servers can handle that
      • hillaj joined the channel
      • mattmcc
        Famous last words.. :)
      • MarkusH
        haha, yes
      • anyways, I gotta go. Bed time
      • aviraldg has quit
      • And for those of you using Uber instead of taxi companies: change your password: http://motherboard.vice.com/read/stolen-uber-cu...
      • autrilla_ joined the channel
      • autrilla_
        Disconnected, the last message I sent was <autrilla__> Well, in my case each query wouldn't have to handle more than 500 rows at a time, I think all servers can handle that
      • masterrex
        hi
      • i have a db-related django question
      • autrilla_
        I do understand it's slower but, only if you do queries that require recursion
      • , but*
      • Genitrust has quit
      • masterrex
        how would I do something like add a unique constraint to a PAIR of fields? i.e. I have a "portion" class that has properties for size(0-9) and unit (oz/g/kg) with a foreign key to a food item
      • i would not want it to be possible to create duplicate portions for the same food
      • autrilla__ has quit
      • mattmcc
        masterrex: unique_together
      • masterrex
        <3
      • =)
      • mysteryjeans joined the channel
      • that's exactly what i'm after
      • yuciyuar has quit
      • tusind tak
      • autrilla_
        ok this is weird
      • bkuberek joined the channel
      • brockhaywood has quit
      • brockhaywood joined the channel
      • snurfery joined the channel
      • codeme joined the channel
      • Debnet has quit
      • meisth0th has quit
      • masterrex
        hum, another dumb question... let's say I have a choices tuple that I want to utilize for multiple classes... apparently I can't just define it at the top of the models.py module
      • TypeError: 'tuple' object is not callable
      • mattmcc
        Sure you can.
      • doismellburning
        masterrex: welcome to #django please dpaste code etc.
      • masterrex
      • doismellburning: I've been a nuisance here for a while :P