私はDjangoユーザーモデルを拡張するBcustomerアプリケーションの顧客モデルを持っています。そこで、Userテーブルの名前やその他のデータ(都市など)などの基本的な詳細を保存します顧客テーブル。django rest framework:ネストされたモデルは機能しません。 'str'オブジェクトに属性 'values'がありません
保存は完全に動作しています。しかし、今私はGETメソッドを呼び出すときに次のエラーを表示しています。
AttributeError at /api/v1/customer
'str' object has no attribute 'values'
Request Method: GET
bcustomer/models.py views.py
class BCustomer(models.Model):
customer = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, primary_key=True, blank=True)
address = models.CharField(max_length=50)
city = models.CharField(max_length=256)
state = models.CharField(max_length=50)
user = models.ForeignKey(settings.AUTH_USER_MODEL, db_index=True, on_delete=models.CASCADE, related_name='customer_creator')
# more fields to go
def __str__(self):
# return str(self.name) (This should print first and last name in User model)
class Meta:
app_label = 'bcustomer'
bcusomer/serializers.py
class CustomerDetailsSerializer(serializers.ModelSerializer):
class Meta:
model = BCustomer
fields = ('city', 'phone')
class CustomerSerializer(serializers.ModelSerializer):
customer_details = CustomerDetailsSerializer()
class Meta:
model = get_user_model()
fields = ('id','first_name', 'email', 'customer_details')
def create(self, validated_data):
request = self.context.get('request')
customer_details_data = validated_data.pop('customer_details')
customer_user = get_user_model().objects.create(**validated_data)
BCustomer.objects.create(customer=customer_user, user=request.user, **customer_details_data)
customer_user.customer_details = customer_details_data
return customer_user
class CustomerListSerializer(serializers.ModelSerializer):
model = get_user_model()
fields = '__all__'
class Meta:
model = get_user_model()
fields = '__all__'
bcustomer/
class CustomerViewSet(viewsets.ModelViewSet):
customer_photo_thumb = BCustomer.get_thumbnail_url
permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope]
queryset = BCustomer.objects.all()
serializer_class = CustomerSerializer
def get_queryset(self):
queryset = BCustomer.objects.all()
return queryset
def get_serializer_class(self):
if self.action == 'list' or self.action == 'retrieve':
return CustomerListSerializer
return CustomerSerializer
bcustomer/urls.py
router.register(r'customer', views.CustomerViewSet, 'customers')
データポストパラメータ形式
{
"first_name":"Myname",
"email":"[email protected]",
"customer_details": {
"city":"citys",
"phone":"04722874567",
}
}
'CustomeListSerializer'の中に' model = get_user_model() 'と' fields = '__all __' 'があるのはなぜですか?すでにメタクラスに入っています。 –