tickets: 3369
This data as json
id | created | changetime | last_pulled_from_trac | stage | status | component | type | severity | version | resolution | summary | description | owner | reporter | keywords | easy | has_patch | needs_better_patch | needs_tests | needs_docs | ui_ux |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
3369 | 2007-01-26 06:31:13 | 2007-09-16 16:13:51 | 2022-03-06 03:28:31.822843 | Design decision needed | closed | Database layer (models, ORM) | dev | duplicate | reverse caching of foreign keys | It would be nice if Django were to cache the reverse value of foreign keys when they're looked up. That might not be entirely clear, so let me give an example: {{{ class Company(Model): name = models.CharField(maxlength=100) class Employee(Model): company = models.ForeignKey(Company) c = Company.objects.all()[0] e = c.employee_set.all()[0] # e now really must have a company of c... there's no reason it wouldn't be c e.company # accesses the database }}} I tried implementing this, but got lost in some of the model code. My approach was to use an auto_cache 'filter' for QuerySets. The above code would look like this: {{{ c = Company.objects.all()[0] e = c.employee_set.auto_cache(company=c).all()[0] e.company # no database accesses }}} Which eventually I would have made automatic for all sets. I got far enough to realize that each foreign key will potentially query the database even after an attribute has explicitly been assigned. For example: {{{ c = Company.objects.all()[0] e = c.employee_set.all()[0] new_company = Company() e.company = new_company e.company == new_company # database accesses for e.company and not equal to the new object }}} I'd love to see a feature like this, so if it's not something that you all want to implement, point me in the right direction. | nobody | wbyoung@mcdonogh.org | 0 | 1 | 0 | 0 | 0 | 0 |