For posterity, I discovered the difference between 0.96 and 1.1.1. Django 0.96 doesn't cache related objects passed in via the constructor of a model, whereas 1.1.1 does.
Here is the setup. I create a model instance, just populating the primary key: >>> s = Station(station_index=1846) In 0.96 I was doing the following. "station" on the StationRel object is a ForeignKey: >>> badrel = StationRel(station=s) For reference, here is the ORM-generated relation: >>> goodrel = StationRel.objects.get(station__station_index=1846) In Django 0.96, the model sucks out the primary key from the Station object, but discards the Station object itself: >>> badrel.__dict__ {'id': None, 'station_id': 1846} >>> goodrel.__dict__ {'id': 4255L, 'station_id': 1846L} This causes references to the "station" foreign key on StationRel to load the full Station model object. In both cases, the station object gets loaded. However, in Django 1.1.1, when a model object involved in a ForeignKey is passed in, it caches that object, assuming it's fully populated. Note the '_station_cache' property: >>> badrel.__dict__ {'_station_cache': <Station: None [None]>, 'station_id': 1846, 'id': None} >>> goodrel.__dict__ {'id': 9L, 'station_id': 1846L} So when the station property is accessed on the relation, it uses the cached object, which isn't populated when "StationRel.objects.get" is used. The solution for me is to just pass in 'station_id'' to the constructor like: >> badrel_nowgood = StationProviderRel(station_id=1846) >> badrel_nowgood.__dict__ {'id': None, 'station_id': 1846} and not create a Station object at all. Best Regards, Eric On Feb 24, 3:28 pm, Eric Floehr <e...@intellovations.com> wrote: > I am moving an application (finally) from 0.96 to 1.1.1 and I'm > running into an issue I haven't been able to resolve. > > To make things simple, here are the two relevant models: > > class Station(models.Model): > station_index = models.AutoField(primary_key=True) > name = models.CharField() > > ... > > class StationRelation(models.Model): > station = models.ForeignKey(Station, db_column='station_index') > > objects = CustomStationRelationManager() > ... > > class CustomStationRelationManager() > def get_nearest(zipcode): > .... > > In the custom manager code, I set the primary key in a new Station > object so it (in 0.96) would only get loaded if it was referenced: > > def get_nearest(zipcode): > ... custom SQL that returns a station_index, etc. ... > s = Station(station_index=row[0]) # row[0] is select > station_index, from the sql result > rel = self.model(station=s) > return rel > > In 0.96, if code that received the StationRelation object accessed, > say the name field, the Django ORM would do sql to fully populate the > station object. > > print rel.station.name > COLUMBUS > > However, in 1.1.1, the same code does not do that: > > print rel.station.name > None > > In either 0.96 or 1.1.1 if I use the standard manager, lazy > instantiation works as normal: > > rel = StationRelation.objects.get(station__station_index=1846) # Note, > no select_related > ... > print rel.station.name # the SQL to get station's properties happens > here, not before > COLUMBUS > > So, the question is, what do I have to do in 1.1.1 that I didn't in > 0.96 in order to properly lazy populate a manually created model > object, beyond just the primary key? > > Thanks for your help! > Eric -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.