python - Grouping Django Model Instances -
i've looked around , can't find this, i'm asking...
i have database 50 states , populations stored in integer field. none of populations same.
i create 10 groups of 5 states ordered population size, 5 populous states in group one, 5 next populous in group two, , on.
i'm creating data migration form these groups , save groupings , i'm not sure how iterate on instances.
size_of_group = 5 all_states = state.objects.order_by('-population') grouped_states = [all_states[i:i + size_of_group] in xrange(0, len(all_states), size_of_group)]
it give list of sublists. each sublist contains size_of_group
states. grouped_states[0]
have 5 biggest populations. '-population'
says order descending.
assuming model looks like:
class state(models.model) population = models.integerfield()
Comments
Post a Comment