【发布时间】:2021-07-30 21:56:40
【问题描述】:
我在尝试调用 API http://127.0.0.1:8000/people/create-by-god/ 时收到错误'Person' object has no attribute '_default_manager'。 而且我不知道为什么,因为我将 null、blank 和 default 设置为我不发送请求的值。 帮我!请
这是我的来源 型号
class Person(models.Model):
GENDER = [
('XX', 'Female'),
('XY', 'Male'),
]
STATUS = [
(0, 'Death'),
(1, 'Alive'),
]
first_name = models.CharField(max_length=50, blank=False, null=False)
last_name = models.CharField(max_length=50, blank=False, null=False)
gender = models.CharField(max_length=10, choices=GENDER,blank=False, null=False)
father = models.ForeignKey('self', on_delete=models.CASCADE, related_name='+', null=True, blank=True)
mother = models.ForeignKey('self', on_delete=models.CASCADE, related_name='+', null=True, blank=True)
job = models.ForeignKey(Job, on_delete=models.CASCADE, null=True, blank=True)
local = models.CharField(max_length=20)
statu = models.IntegerField(choices=STATUS, default=1)
date_of_birth = models.DateField(auto_now_add=True)
date_of_death = models.DateField(null=True, blank=True)
def __str__(self):
return '%s %s' % (self.first_name, self.last_name)
序列化器
class FirstPersonSerializer(serializers.ModelSerializer):
class Meta:
model = Person()
fields = ['first_name', 'last_name', 'gender', 'local']
观看次数
class PersonViewSet(viewsets.ModelViewSet):
queryset = Person.objects.all()
serializer_class = PersonSerializer
@action(methods=['post'], detail=False, url_path='create-by-god', url_name='create-by-god')
def create_by_god(self, request, *args, **kwargs):
self.serializer_class = FirstPersonSerializer
return super().create(request, *args, **kwargs)
错误日志
File "C:\Users\laptop\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\laptop\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\laptop\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\laptop\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\laptop\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "C:\Users\laptop\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\laptop\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "C:\Users\laptop\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "C:\NhatNDQ\training\training-django\my_big_world\world\views\person.py", line 13, in create_by_god
return super().create(request, *args, **kwargs)
File "C:\Users\laptop\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\mixins.py", line 18, in create
serializer.is_valid(raise_exception=True)
File "C:\Users\laptop\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\serializers.py", line 220, in is_valid
self._validated_data = self.run_validation(self.initial_data)
File "C:\Users\laptop\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\serializers.py", line 421, in run_validation
self.run_validators(value)
File "C:\Users\laptop\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\serializers.py", line 454, in run_validators
super().run_validators(to_validate)
File "C:\Users\laptop\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\fields.py", line 578, in run_validators
for validator in self.validators:
File "C:\Users\laptop\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\fields.py", line 408, in validators
self._validators = self.get_validators()
File "C:\Users\laptop\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\serializers.py", line 1500, in get_validators
self.get_unique_for_date_validators()
File "C:\Users\laptop\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\serializers.py", line 1576, in get_unique_for_date_validators
default_manager = self.Meta.model._default_manager
AttributeError: 'Person' object has no attribute '_default_manager'
【问题讨论】:
-
请将完整的错误回溯添加到您的问题中!
-
您可以尝试删除两个
mother/father外键上的related_name值吗? -
我尝试删除它们并得到错误
ERRORS: world.Person.father: (fields.E304) Reverse accessor for 'Person.father' clashes with reverse accessor for 'Person.mother'. HINT: Add or change a related_name argument to the definition for 'Person.father' or 'Person.mother'. world.Person.mother: (fields.E304) Reverse accessor for 'Person.mother' clashes with reverse accessor for 'Person.father'. HINT: Add or change a related_name argument to the definition for 'Person.mother' or 'Person.father'.
标签: python api django-rest-framework