0

I have a basic model which references both ForeignKeys and ManyToMany objects. In "edit" testing where you are taking the id of a view and making changes to it I ran into a problem and I'm curious if anyone else figured out a cleaner workaround. I found this post which started me down the right path

    client = Client()
    response = client.get(reverse("floorplan_update", kwargs={'pk': floorplan.id}))

    data = response.context['form'].initial

    # Ideally you should be able to do this..
    response = client.post(reverse("floorplan_update", kwargs={'pk': floorplan.id}),
                           data=data, follow=True)

But you can't do this. In cases where you have FK's or M2M's you need to first do this ugliness...

    client = Client()
    response = client.get(reverse("floorplan_update", kwargs={'pk': floorplan.id}))

    data = response.context['form'].initial

    # Ugliness ensues..
    data['document'] = open(__file__)
    data['company']= data['company'].id
    data['target']= data['target'].id

    # Only now can you post..

    response = client.post(reverse("floorplan_update", kwargs={'pk': floorplan.id}),
                           data=data, follow=True)

Has anyone else ran into this or is there a better way to deal with this?

Community
  • 1
  • 1
rh0dium
  • 6,811
  • 4
  • 46
  • 79

1 Answers1

1

Not sure, but you can try this instead:

data = response.context['form'].instance.__dict__
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444