0

Hi Stackoverflow people,

I am experiencing a strange issue with a select_related query in Django. I have installed the django-cities app, which lists geo information for a large number of cities. In my project model, I have created a foreignkey to the city element to store the location in my models.py.

from cities.models import City 
class Project(models.Model): 
   ...
   city = models.ForeignKey(City, blank = True, null = True)

Due to the large number of possible cities, I have created a query in my class based view which selects the related city field when creating a Project object.

from django.views.generic.edit import CreateView, UpdateView, DeleteView
from project.models import Project
class ProjectCreate(CreateView):
    queryset = Project.objects.select_related('city__country', 'city__region').all()
    template_name = 'solution/solution_create_form.html'

However, when I request the ProjectCreate class, Django still pulls an endless number of foreignkeys from the database. The logger is showing an unlimited number of the following requests.

... DEBUG (0.000) SELECT "cities_region"."id", "cities_region"."name", "cities_region"."slug", "cities_region"."name_std", "cities_region"."code", "cities_region"."country_id" FROM "cities_region" WHERE "cities_region"."id" = 3861887 ; args=(3861887,) DEBUG (0.000) SELECT "cities_country"."id", "cities_country"."name", "cities_country"."slug", "cities_country"."code", "cities_country"."population", "cities_country"."continent", "cities_country"."tld" FROM "cities_country" WHERE "cities_country"."id" = 3865483 ; args=(3865483,) D ...

How can I force the select related method when I execute the ProjectCreate class?

Thank you for your help and suggestions!

neurix
  • 4,126
  • 6
  • 46
  • 71
  • show the debug info (SQL) for your Project.objects... query – Dmitry B. Aug 19 '12 at 06:58
  • Interestingly when I execute `from django.db import connection print "Query %s" % connection.queries`, the query is empty. Is there a better way to display the query? Django-toolbar does not work since the site is not fully loaded when the sql requests go wild. – neurix Aug 19 '12 at 07:09
  • there is a different way. Find base.py for your database's driver. On my system, for example, Postgres's base.py is located at . Then find method execute() inside of class CursorWrapper. Add a new line at the start of the method that print method's arguments: query and args. – Dmitry B. Aug 19 '12 at 07:19
  • Hm, i get a similar result. Before the sql request goes wild, only one other model is requested. Results: `SELECT postgis_lib_version() None SELECT "product_subcategory"."id", "product_subcategory"."category", "product_subcategory"."name", "product_subcategory"."long_name", "product_subcategory"."slug" FROM "product_subcategory" () SELECT "cities_city"."id", "cities_city"."name", "cities_city"."slug", "cities_city"."name_std", "cities_city"."location", "cities_city"."population", "cities_city"."region_id", "cities_city"."subregion_id", "cities_city"."country_id" FROM "cities_city" () ` – neurix Aug 19 '12 at 07:35
  • are you by any chance populating the create form's dropdowns with city/region names? – Dmitry B. Aug 19 '12 at 07:47
  • Hi Dmitry, yes, I the form includes a drop down menu for the foreignkeys in the Project model. Would I have to change anything in the form creation? – neurix Aug 19 '12 at 19:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/15541/discussion-between-dmitry-beransky-and-neurix) – Dmitry B. Aug 19 '12 at 19:31

1 Answers1

3

Without any testing, have you tried overriding the get_queryset method instead of using the queryset attribute? At least you will be more flexible with exception catching and other techniques.

Berislav Lopac
  • 16,656
  • 6
  • 71
  • 80