title: Using setattr() to update an object instance
body: <p>I have a model class which has an attribute that refers to django DB objects. I would like to change this attribute using a single view with setattr() which I use to make changes to any attributes for this object.</p>
  
  <p>The problem is, I can't seem to pass an object instance through the stack. I'm not sure if I can even use setattr() for this. Actually I'm not even sure if the problem is with my attempted use of setattr() or something else - please let me know!</p>
  
  <p>Error on POST attempt: </p>
  
  <pre><code>ValueError at /dollhouseupdate/1
  Cannot assign "u'Citadel'": "Dollhouse.dh_background" must be a "Background" instance.
  </code></pre>
  
  <p>Model: </p>
  
  <pre><code>class Dollhouse(models.Model):
      dollhouse_name = models.CharField(max_length=100)
      user = models.ForeignKey(User)
      dh_background = models.ForeignKey(Background)
      def __str__(self):
          return self.dollhouse_name
  </code></pre>
  
  <p>Template:</p>
  
  <pre><code>    &lt;select id="background-select"&gt;
        &lt;option value="null"&gt;Change Background&lt;/option&gt;
          {% for background in background_objects %}
          &lt;option value="{{ background }}"&gt;{{ background.bg_name }} &lt;/option&gt;
          {% endfor %}
      &lt;/select&gt;
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>def dollhouseupdate(request, dollhouseid):
      if request.method == 'POST':
          workingdollhouse = Dollhouse.objects.get(id=dollhouseid)
          if request.POST.get('erase') == "true":
              workingdollhouse.delete()
              return HttpResponse("Dollhouse deleted!")
          else:
              data = (request.POST).dict()
              for key, value in data.items():
                  setattr(workingdollhouse, key, value)
              workingdollhouse.save()
              return HttpResponse("Dollhouse {} saved!".format(workingdollhouse.dollhouse_name))
  </code></pre>
  
  <p>Javascript:</p>
  
  <pre><code>//change dollhouse background
  $("#background-select").change(function() {
      if($("#background-select").val() != null) {
          var dollhouseid = workingDollhouse;
          var dh_background = $("#background-select").val()
          console.log("changing background to " + dh_background);
          $.ajax("http://127.0.0.1:8000/dollhouseupdate/"+dollhouseid, {
              type: 'POST',
              data: {
                  dh_background: dh_background,
              }
          })
          .done(function(response){
             console.log("The request is complete!");
             console.log(response);
             window.location = "http://127.0.0.1:8000/";
         })
         .fail(function() {
             console.log("Sorry, there was a problem!");
         })
      };
  });
  </code></pre>