When I try to query foreign keys using get(), I always get None values, even though I know they are set to 1 in the DB. Am I missing something here? Should I do something different to get the foreignkey values?
Here’s what the code looks like:
class Car_to_brand( models.Model ):
brand_id = models.ForeignKey( Brand, db_column='brand_id' )
...
class Brand( models.Model ):
(id is not defined here but it's the primary key)
...
print Car_to_brand.__dict__.get( brand_id )
That would give me {brand_id:None}, but it should be {brand_id:1}.
The problem is that you have named your field
brand_idand not brand.get(brand_id)is returning None, because there the keybrand_idis not in the dictionary. If you printcar.__dict__, you will see that it containsbrand_id_idinstead.However, it is highly unusual to access attributes using
instance.__dict__.get(). Try the following instead: