python - Djangonic way to get second order DB-info in Django? -


i'm messing around first django site , far it's going good. facing challenge of getting information db. model looks this:

class countries(models.model):     country = models.charfield(max_length=100)     def __unicode__(self):         return self.country  class organisationtypes(models.model):     organisation_type = models.charfield(max_length=100)     def __unicode__(self):         return self.organisation_type  class organisations(models.model):     organisation_name = models.charfield(max_length=200)     organisation_type = models.foreignkey(organisationtypes)     country_of_origin = models.foreignkey(countries)     def __unicode__(self):         return self.organisation_name  class locations(models.model):     organisation = models.foreignkey(organisations)     country_of_location = models.foreignkey(countries)     tel_nr = models.charfield(max_length=15)     address = models.charfield(max_length=100)     def __unicode__(self):         return '%s - %s - %s - %s' % (self.organisation, self.country_of_location, self.tel_nr, self.address) 

i want display list of locations of want display organisation_name , country_of_origin. achieve wrote following function:

def organisation_locations(requests, organisation_id):     org = organisations.objects.get(id=organisation_id)     location_list = locations.objects.filter(organisation=organisation_id).order_by('country_of_location')     output = '<br />'.join([str(loc.organisation)+' '+str(org.country_of_origin) loc in location_list])     return httpresponse(output) 

this works correctly, doesn't seem correct way of doing this. since location table has foreign key in organisations table in turn has foreign key in countries table have vague feeling django can in 1 "query" or lookup.

am correct in feeling, or way indeed correct way of doing this? tips welcome!

can't do:

location_list = locations.objects\                  .filter(organisation=organisation_id)\                  .order_by('country_of_location') output = '<br />'.join([str(loc.organisation)+' '+str(loc.organisation.country_of_origin) loc in location_list]) 

the organisation query isn't necessary. can access organisation this: localization.organisation.

what not djangonic in code response. should have template , return render_to_response :)


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -