我正在嘗試使用API插入數據,並且想要管理將數據傳遞給外鍵的方式。不過,我不斷收到空值,即使他們不在束空Django中的外鍵管理Tastypie
MODELS.PY
class LCUser(models.Model):
email = models.CharField(max_length=100,unique=True)
password = models.CharField(max_length=100)
first_name = models.CharField(max_length=100)
last_name=models.CharField(max_length=100)
class Meta:
db_table = "user"
class UserProfile(models.Model):
user = models.ForeignKey(LCUser, on_delete=models.CASCADE,primary_key=True)
mobile_phone = models.IntegerField()
city=models.CharField(max_length=50)
class Meta:
db_table = "profile"
API.PY
class MultipartResource(object):
def deserialize(self, request, data, format=None):
if not format:
format = request.META.get('CONTENT_TYPE', 'application/json')
if format == 'application/x-www-form-urlencoded':
return request.POST
if format.startswith('multipart/form-data'):
multipart_data = request.POST.copy()
multipart_data.update(request.FILES)
return multipart_data
return super(MultipartResource, self).deserialize(request, data, format)
def put_detail(self, request, **kwargs):
if request.META.get('CONTENT_TYPE', '').startswith('multipart/form-data') and not hasattr(request, '_body'):
request._body = ''
return super(MultipartResource, self).put_detail(request, **kwargs)
def patch_detail(self, request, **kwargs):
if request.META.get('CONTENT_TYPE', '').startswith('multipart/form-data') and not hasattr(request, '_body'):
request._body = ''
return super(MultipartResource, self).patch_detail(request, **kwargs)
class ProfileResource(MultipartResource,ModelResource):
#Get the id from the request and obtain the correcct user object
user_id = LCUser.objects.get(id=1) #For testing
class Meta:
queryset = UserProfile.objects.all()
resource_name = 'profile'
authorization = Authorization()
def obj_create(self,bundle,request=None,**kwargs):
try:
print repr(bundle)
bundle = super(ProfileResource, self).obj_create(bundle, request=request, **kwargs)
bundle.obj.save()
except:
traceback.print_exc()
raise BadRequest('Error')
return bundle
REQUEST
POST /api/profile/? HTTP/1.1
Host: 127.0.0.1:8000
Cache-Control: no-cache
Postman-Token: c3cddfd3-a2ec-06fc-e916-ed5ff8f21077
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="user"
1
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="mobile_phone"
123
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="city"
123
當我嘗試插入我得到一個完整性錯誤, IntegrityError:列「user_id」中的空值違反不是空約束 失敗行包含(NULL,123,123)
束
<Bundle for obj: 'UserProfile object' and with data: '{'mobile_phone': u'123', 'user': u'1', 'city': u'123'}'>
但是,您是否「修復」了您的問題,或者您是否實施了「黑客」?正如Sean指出的那樣,修復引用的正確方法是在資源中定義該引用。 –