gpt4 book ai didi

python - django.core.exceptions.ImproperlyConfigured : Field name `id` is not valid for model

转载 作者:行者123 更新时间:2023-12-01 02:01:01 30 4
gpt4 key购买 nike

我的一个序列化器遇到了问题。我试图返回 PriceUnit 对象的所有数据,但 DRF 拒绝提供 id。这个id是自动生成的,我不会修改它。

序列化器.py

class PriceUnitSerializer(serializers.ModelSerializer):
product_id = serializers.SerializerMethodField()

def get_product_id(self, obj):
if obj.product is not None:
return obj.product.id
return None

class Meta:
model = PriceUnit
fields = ('id',
'name',
'formula',
'product_id')

urls.py

url(r'^price_units/$', price_unit_view.PriceUnitCreateUpdate.as_view()),
url(r'^price_units/(?P<pk>[0-9]+)/$', price_unit_view.PriceUnitList.as_view()),

models.py

class PriceUnit(models.Model):
UNIT = 'Unit'
SQUAREMATER = 'm2'
CUBEMETER = 'm3'
LINEARMETER = 'ml'
KILOGRAM = 'kg'

PRICE_UNITS_CHOICES = (
(UNIT, 'Unit'),
(SQUAREMATER, 'm2'),
(CUBEMETER, 'm3'),
(LINEARMETER, 'ml'),
(KILOGRAM, 'kg'),
)

name = models.CharField(max_length=50, choices=PRICE_UNITS_CHOICES, default=UNIT,)
formula = models.CharField(max_length=400, blank=True)
product = models.OneToOneField(Product, on_delete=models.CASCADE, primary_key=True,)

def __str__(self):
return 'Price unit : ' + self.name + ' - Product #' + str(self.product.pk)

price_unit_view.py

class PriceUnitList(APIView):
"""
Retrieve a PriceUnit.
"""

permission_classes = (permissions.IsAuthenticated,)

def get(cls, request, pk, format=None):
price_unit_list = PriceUnit.objects.filter(pk=pk)

if price_unit_list:
serializer = PriceUnitSerializer(price_unit_list[0])
return Response(serializer.data, status=status.HTTP_200_OK)
else:
return Response(data={}, status=status.HTTP_204_NO_CONTENT)

请求(使用 httpie):

http GET http://127.0.0.1/api/price_units/1/ "Authorization: Token ba4ee2628669a9cc0d6e715b12660003f748c674"

错误:

ERROR [django.request:135] Internal Server Error: /price_units/1/
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 494, in dispatch
response = self.handle_exception(exc)
File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 454, in handle_exception
self.raise_uncaught_exception(exc)
File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 491, in dispatch
response = handler(request, *args, **kwargs)
File "./main/views/price_unit_view.py", line 27, in get
return Response(serializer.data, status=status.HTTP_200_OK)
File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py", line 537, in data
ret = super(Serializer, self).data
File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py", line 262, in data
self._data = self.to_representation(self.instance)
File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py", line 487, in to_representation
fields = self._readable_fields
File "/usr/local/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py", line 377, in _readable_fields
field for field in self.fields.values()
File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py", line 363, in fields
for key, value in self.get_fields().items():
File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py", line 1022, in get_fields
source, info, model, depth
File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py", line 1167, in build_field
return self.build_unknown_field(field_name, model_class)
File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py", line 1279, in build_unknown_field
(field_name, model_class.__name__)
django.core.exceptions.ImproperlyConfigured: Field name `id` is not valid for model `PriceUnit`.

我的其他序列化器与这个完全相同,但是可以返回 id 并且不会造成任何问题。

我们将不胜感激您的帮助。

最佳答案

正如错误所示,您的模型没有 id 字段。它具有三个字段:nameformulaproductproduct 字段具有 primary_key=True,因此 Django 不会自动创建 id 字段。

关于python - django.core.exceptions.ImproperlyConfigured : Field name `id` is not valid for model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49620189/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com