title: DoesNotExist while creating new object - tastypie
body: <p>models.py</p>
  
  <pre><code>class ModelType(models.Model):
      Name = models.CharField("Name", max_length=50)
  
      def __unicode__(self):
          return unicode(self.Name)
  
  
  class BaseModel(models.Model):
      ModelType = models.ForeignKey(ModelType)
      Created = models.DateTimeField()
      Modified = models.DateTimeField()
  
      def __unicode__(self):
          return unicode(self.id)
  
      def save(self, *args, **kwargs):
          print 'inside basemodel save'
  
          if self.ModelType==None:
              try:
                  self.ModelType = ModelType.objects.get(Name=self.__class__.__name__)
              except:
                  m = ModelType(Name=self.__class__.__name__)
                  m.save()
                  self.ModelType = m
          if self.id in [None, '']:
              self.Created = datetime.datetime.now()
          self.Modified = datetime.datetime.now()
  
          print self.ModelType
          super(BaseModel, self).save(*args, **kwargs)
  
  class Patient(BaseModel):
      Name = models.CharField('Name', max_length=100)
  </code></pre>
  
  <p>resource.py</p>
  
  <pre><code>class ModelTypeResource(ModelResource):
      class Meta:
          queryset = ModelType.objects.all()
          filtering = {"Modified": ALL}
          authorization = Authorization()
          always_return_data = True
  
  
  class BaseModelResource(ModelResource):
      ModelType = fields.ForeignKey(ModelTypeResource, 'ModelType', full=False, null=True)
      class Meta:
          queryset = BaseModel.objects.all()
          filtering = {"Modified": ALL, 'ClinicDevice': ALL, 'ModelType': ALL}
          authorization = Authorization()
          always_return_data = True
  
  
  class PatientResource(ModelResource):
      ModelType = fields.ForeignKey(ModelTypeResource, 'ModelType', full=False, null=True)
  
      class Meta:
          queryset = Patient.objects.all()
          filtering = {"Modified": ALL}
          authorization = Authorization()
          always_return_data = True
  </code></pre>
  
  <p>Now, if I execute following command to add a <code>Patient</code></p>
  
  <pre><code>curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"Name":"p1"}' http://localhost:8001/api/v1/patient/
  </code></pre>
  
  <p>following error is thrown</p>
  
  <pre><code>HTTP/1.0 404 NOT FOUND
  Date: Wed, 29 Jan 2014 14:10:59 GMT
  Server: WSGIServer/0.1 Python/2.7.3
  Content-Type: application/json
  
  {
      "error_message": "", 
      "traceback": "
  
  Traceback (most recent call last):
  
    File "/usr/local/lib/python2.7/dist-packages/tastypie/resources.py", line 195, in wrapper
      response = callback(request, *args, **kwargs)
  
    File "/usr/local/lib/python2.7/dist-packages/tastypie/resources.py", line 426, in dispatch_list
      return self.dispatch('list', request, **kwargs)
  
    File "/usr/local/lib/python2.7/dist-packages/tastypie/resources.py", line 458, in dispatch
      response = method(request, **kwargs)
  
    File "/usr/local/lib/python2.7/dist-packages/tastypie/resources.py", line 1320, in post_list
      updated_bundle = self.obj_create(bundle, **self.remove_api_resource_names(kwargs))
  
    File "/usr/local/lib/python2.7/dist-packages/tastypie/resources.py", line 2083, in obj_create
      bundle = self.full_hydrate(bundle)
  
    File "/usr/local/lib/python2.7/dist-packages/tastypie/resources.py", line 876, in full_hydrate
      value = field_object.hydrate(bundle)
  
    File "/usr/local/lib/python2.7/dist-packages/tastypie/fields.py", line 735, in hydrate
      value = super(ToOneField, self).hydrate(bundle)
  
    File "/usr/local/lib/python2.7/dist-packages/tastypie/fields.py", line 166, in hydrate
      elif self.attribute and getattr(bundle.obj, self.attribute, None):
  
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 389, in __get__
      raise self.field.rel.to.DoesNotExist
  
  DoesNotExist
  
  
  "}
  </code></pre>
  
  <p>I have added the <code>ForeignKey</code> relation for <code>ModelType</code> in resource of <code>Patient</code>. And the value of <code>ModelType</code> is set inside save method of <code>BaseModel</code>. I am not able to figure out where exactly the error is.</p>