1
這是我的設置:嵌套tastypie UserProfileResource和UserResource,在列空值「user_ID的」違反了非空約束
class UserProfile(models.Model):
user = models.OneToOneField(User, unique=True, related_name="profile")
class UserProfileResource(ModelResource):
class Meta:
queryset = UserProfile.objects.all()
resource_name = 'profile'
authorization = Authorization()
class UserResource(ModelResource):
profile = fields.ToOneField(UserProfileResource, attribute='userprofile', related_name='user', full=True, null=True)
class Meta:
queryset = User.objects.all()
resource_name = 'user'
authorization = Authorization()
list_allowed_methods = ['get', 'post']
我試圖張貼到用戶資源:
curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"username":"tata","password":"poooo","profile":{"home_zipcode": "95124"}}' http://192.168.1.103:8000/api/v1/user/
但得到以下錯誤:
"error_message": "null value in column \"user_id\" violates not-null constraint\