Django: Creating bound form from model instance
===

I am trying to write my first unit test in Django. It's for a Staff registration form.
The Staff model for the form has a OneToOne relation with a UserProfile (AUTH_PROFILE_MODULE).
The UserProfile has a OneToOne relation with django.contrib.auth.models.User.

I am using https://github.com/dnerdy/factory_boy to create a test model instance for the staff model. The idea is to use a StaffFactory so I can easily create test model instances. To create a bound form I need to pass it a data dict. I thought it would be convenient to just use **django.forms.models.model_to_dict** to convert my model instance into a data dict when testing the form. 

Now, my problem is: model_to_dict does not traverse the foreign keys of my Staff model (Staff->UserProfile->User). This means the form stays invalid since required fields like the User's email are still missing inside the form data.

Currently my StaffRegistrationFormTest looks like:


    class StaffRegistrationFormTest(unittest.TestCase):
      def test_success(self):
        staff1 = StaffFactory()

        form = StaffRegistrationForm(model_to_dict(staff1))
        # print jsonpickle.encode(model_to_dict(staff1))
        self.assertTrue(form.is_valid(), form.errors)


Is there a way to pass in a dict, where the foreign keys are serialized by re-using a model instance?

