[英]Get Error '...' object has no attribute '_default_manager' when call API
I get error 'Person' object has no attribute '_default_manager' when try to call API http://127.0.0.1:8000/people/create-by-god/ .尝试调用 API http://127.0.0.1:80时,我收到错误“人”object 没有属性“_default_manager” 。 And I don't know why, because I set null, blank and default at values I dont send in request.
而且我不知道为什么,因为我将 null、空白和默认值设置为我不发送请求的值。 Help me!
帮我! please
请
Here is my source Model这是我的来源 Model
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)
Serializer串行器
class FirstPersonSerializer(serializers.ModelSerializer):
class Meta:
model = Person()
fields = ['first_name', 'last_name', 'gender', 'local']
Views意见
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)
Error log错误日志
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'
I fixed it!我修好了它! I just remove
()
like that:我只是像这样删除
()
:
class FirstPersonSerializer(serializers.ModelSerializer):
class Meta:
model = Person #before is Person()
fields = ['first_name', 'last_name', 'gender', 'local']
Sin paréntesis, gracias funciono Sin paréntesis, gracias funciono
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.