Two small issues I run into when dealing with OneToOneField and iterating through a model's fields.
One to One Fields
When models are defined like above, you can grab a reference to B this way:
Since B's primary key equals to A's primary key, when Django's ORM creating B in the database, it will not create an 'id' column. Instead, a column named 'A_id' is created. Thus, when ever you want to access B's primary key, you cannot do:
You have to use b_instance.pk instead.
Model's _meta field
I once had a task which needs to determine if any field within a model instance is empty. To do it, I need a way to loop through all of the fields defined in the instance first:
Here is the code for determining if a model instance is empty: