Django foreign key query, why it returns None? -


When I try to query the foreign key using get (), then always Values ​​are not found, however I know they are set to 1 in DB. Am I missing something here? Should I do something different to get foreign key values?

This code looks like this:

  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 is the primary key) ... Print Car_to_brand .__ dict __. Get (brand_id)   

This will give me {brand_id: none} , but it should be {brand_id: 1} .

The problem is that you have named your field brand_id and not the brand There is no returning code (brand_id) because the key is not in the brand_id dictionary if you print car.__ signal___ If you do, you will see that it has brand_id_id instead.

However, It is extremely unusual to use features that use example .__ dict __. () . Instead try the following:

  class car (models.Model): brand = models.ForeignKey (brand) # does not need to set db_column, brand_id is the default car. Brand_ID # is the brand ID (such as 1) This car is connected to car.brand # This is a brand instance    

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -