0
当我使用Django REST框架,并使用Use OneToOneField和RetrieveUpdateDestroyAPIView无法通过pk获取; 我的代码如下:Django REST框架:使用OneToOneField不能GET
models.py
class UserAccount(models.Model):
username = models.CharField(null=False, max_length=45)
password = models.CharField(null=False, max_length=16)
class UserContactInfo(models.Model):
userAccount = models.OneToOneField(UserAccount, primary_key=True)
phone_number = models.CharField(null=True, blank=True)
email = models.CharField(null=True, blank=True, max_length=45)
serializers.py
class UserAccountSerializer(serializers.ModelSerializer):
class Meta:
model = UserAccount
fields = ('id', 'username', 'password')
class UserContactInfoSerializer(serializers.ModelSerializer):
class Meta:
model = UserContactInfo
fields = ('userAccount', 'phone_number', 'email')
views.py
class UserContactInfoDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = UserAccount.objects.all()
serializer_class = UserContactInfoSerializer
urls.py
urlpatterns = [
...
url(r'^ContactInfo/(?P<pk>[0-9]+)/$', views.UserContactInfoDetail.as_view()),
]
好吧,当我试图让UserContactInfo数据:
GET http://127.0.0.1:8000/ContactInfo/1/
和出错:
AttributeError at /ContactInfo/1/
'UserAccount' object has no attribute 'userAccount_id'
Request Method: GET
Request URL: http://127.0.0.1:8000/ContactInfo/1/
.....
谁能帮我解决它。谢谢!
谢谢!!这是我粗心的错误! – nghuyong