0:00 AM
MarkusH
other than that, no idea
0:00 AM
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
0:00 AM
rmwdeveloper
+Markush: thanks
0:00 AM
MarkusH
but not sure if partials work with migrations
0:00 AM
Haven't tried that one
0:00 AM
fesener_
Country.objects.order_by('country__name') is not working because it's actually a "CountryField()" rather than a foreignkey
0:02 AM
Goopyo joined the channel
0:03 AM
ruind has quit
0:04 AM
andrew9184 joined the channel
0:05 AM
jessamynsmith
Does Country really have a member country? Or just a member name?
0:05 AM
MarkusH
fesener_: I don't see a way to order on database level
0:05 AM
fesener_
jessamynsmith, sorry i didnt understand the question
0:06 AM
MarkusH, thanks for checking it out anyway :)
0:06 AM
something like "return unicode(self.country.name)" works perfectly but i cant do country__name for ordering
0:06 AM
MarkusH
You might want to watch out for SmileyChris in here ;)
0:06 AM
fesener_
yea I was hoping to find him here :)
0:07 AM
MarkusH
fesener_: django-countries doesn't save the name in the DB
0:07 AM
it is hard coded in python code
0:07 AM
fesener_
ah thats why I cant get __name
0:07 AM
i wish there was a way
0:07 AM
MarkusH
0:08 AM
fesener_
yea :( i guess i cant order them by name, thank you
0:09 AM
MarkusH
0:10 AM
but depending on how may objects you need to sort, I'd just do that on python level
0:10 AM
fesener_
it will be like 30-40 countries at most
0:10 AM
MarkusH
sorted(some_list, key=operator.attrgetter('someattr'))
0:11 AM
fesener_
can you help me adapt that to my Country.objects.order_by() line?
0:11 AM
MarkusH
countries = Country.objects.all() ; sorted(countries, key=operator.attrgetter('name')) ?
0:12 AM
fesener_
let me try
0:12 AM
it says operator is not defined
0:12 AM
MarkusH
import operator
0:13 AM
fesener_
'Country' object has no attribute 'name'
0:13 AM
MarkusH
what is "Country"?
0:14 AM
fesener_
countries = Country.objects.all()
0:15 AM
lefteris has quit
0:15 AM
MarkusH
Let's see your Country model, please
0:16 AM
0:17 AM
codeme has quit
0:17 AM
0:17 AM
That's better
0:18 AM
fesener_
0:18 AM
i guess this is what you are asking?
0:20 AM
autrilla__
Is "for temperature in self.temperature_set.filter(date__gte=start).filter(date__lte=end):" more efficient than .all() and an if afterwards?
0:21 AM
MarkusH
fesener_: ok, make it attrgetter('country.name')
0:21 AM
dray3 has quit
0:21 AM
autrilla__: depends
0:22 AM
fesener_
MarkusH, making it "countries = sorted(countries, key=operator.attrgetter('country.name'))" made it work! thank you so much
0:22 AM
autrilla__
MarkusH, mind elaborating?>
0:22 AM
fesener_
you are awesome
0:22 AM
scofmb_ has quit
0:22 AM
MarkusH
fesener_: thanks ^^, you're welcome
0:22 AM
scofmb joined the channel
0:22 AM
autrilla__: no :-P
0:23 AM
autrilla__
No you don't mind or no you won't elaborate? :p
0:23 AM
MarkusH
autrilla__: well, it probably depends on the amount of data you are querying
0:23 AM
autrilla__
Well not much now, but i production I can expect every user to have thousands of the temperature model
0:23 AM
MarkusH
but I guess the .filter() thing is mostly faster ;)
0:23 AM
autrilla__
in* production
0:24 AM
The .filter runs on the database right?
0:24 AM
MarkusH
yes
0:24 AM
autrilla__
Which is probably faster as it's probably C instead of python
0:24 AM
MarkusH
so you don't have to construct billions of objects
0:24 AM
yes
0:25 AM
autrilla__
Great, thanks
0:31 AM
Sinestro joined the channel
0:33 AM
tWoolie
also, because it uses the database indexes which are pre-sorted
0:33 AM
so make sure you put an index on the name attribute if you're going to be sorting on it
0:35 AM
autrilla__
Can I show related stuff to an object on the admin?
0:36 AM
crakrjak joined the channel
0:37 AM
fesener_ has quit
0:43 AM
jessamynsmith has quit
0:45 AM
0:45 AM
I get __str__ returned non-string (type NoneType)
0:46 AM
mattmcc
Probably because Zone.__str__ doesn't always return a string.
0:47 AM
Incidentally, you may be jumping into using multi-table model inheritance prematurely.
0:47 AM
autrilla__
that's how I'd do it in java
0:48 AM
mattmcc
Well, these classes map to database tables, they're not like ordinary classes.
0:48 AM
So you should be thinking about the DB structure more than what makes sense in Java.
0:49 AM
jessamyn_ joined the channel
0:49 AM
autrilla__
Well, so the Zone class shouldn't me a model?
0:50 AM
Genitrust joined the channel
0:50 AM
MarkusH
0:51 AM
mattmcc
Depends on what other types of zones you have in mind, and how far they nest.
0:51 AM
SQL DBs _suck_ at nesting if you don't use the right structure.
0:51 AM
Siecje has left the channel
0:51 AM
autrilla__
mattmcc, that's basically it... the original plan was one Farm with as many subzones as you want, but just one depth level
0:52 AM
Well, not one Farm, many farms each with many Subzones
0:52 AM
mattmcc
Yeah, then you want models to relate to each other, don't need inheritance to do that.
0:52 AM
hillaj has quit
0:52 AM
schinckel
You may also want __unicode__, rather than __str__.
0:52 AM
MarkusH
You can use an abstract model for common fields
0:52 AM
schinckel: no
0:52 AM
autrilla__
mattmcc, but then Devices can be on either Farms or Subzones
0:52 AM
mattmcc
This is one of those fundamental disconnects between OOP and SQL.
0:52 AM
MarkusH
you want python 3
0:53 AM
schinckel: use __str__ and @python_2_unicode_compatible
0:53 AM
autrilla__
That's why I had a superclass, so Devices could have a Zone
0:53 AM
schinckel
Wonder what I'm doing with my alleged 2-and-3 compatible apps then... ;)
0:55 AM
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?
0:56 AM
MarkusH
autrilla__: yes
0:56 AM
autrilla__
I could probably name it parent instead of farm and let the user decide how much depth he wants
0:57 AM
MarkusH
autrilla__: if you use "my" pattern, you don't want more than 1 level of nesting
0:57 AM
2 at most if you have a medium sized database
0:57 AM
autrilla__
why not/
0:57 AM
?*
0:58 AM
MarkusH
this pattern sucks for relational databases
0:58 AM
mattmcc
When you want to nest more, you want a tree pattern like materialized-path or nested-set.
0:58 AM
Which add extra fields to your models that allow for tree traversal without having to use recursion.
0:59 AM
MarkusH
or, if you want to have arbirary nesting, look at django-treebeard or others (never used that one myself)
0:59 AM
Genitrus_ joined the channel
0:59 AM
mattmcc
Yeah, treebeard provides models for both patterns.
0:59 AM
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
1:00 AM
hillaj joined the channel
1:00 AM
mattmcc
Famous last words.. :)
1:00 AM
MarkusH
haha, yes
1:00 AM
anyways, I gotta go. Bed time
1:01 AM
aviraldg has quit
1:01 AM
1:02 AM
autrilla_ joined the channel
1:02 AM
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
1:02 AM
masterrex
hi
1:02 AM
i have a db-related django question
1:02 AM
autrilla_
I do understand it's slower but, only if you do queries that require recursion
1:02 AM
, but*
1:03 AM
Genitrust has quit
1:04 AM
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
1:04 AM
i would not want it to be possible to create duplicate portions for the same food
1:04 AM
autrilla__ has quit
1:04 AM
mattmcc
masterrex: unique_together
1:04 AM
masterrex
<3
1:04 AM
=)
1:05 AM
mysteryjeans joined the channel
1:05 AM
that's exactly what i'm after
1:05 AM
yuciyuar has quit
1:05 AM
tusind tak
1:07 AM
autrilla_
ok this is weird
1:07 AM
1:07 AM
bkuberek joined the channel
1:07 AM
brockhaywood has quit
1:09 AM
brockhaywood joined the channel
1:10 AM
snurfery joined the channel
1:10 AM
codeme joined the channel
1:11 AM
Debnet has quit
1:12 AM
meisth0th has quit
1:13 AM
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
1:14 AM
TypeError: 'tuple' object is not callable
1:15 AM
mattmcc
Sure you can.
1:15 AM
doismellburning
masterrex: welcome to #django please dpaste code etc.
1:18 AM
masterrex
1:19 AM
doismellburning: I've been a nuisance here for a while :P