我遇到以下行问题:user_brand = UserBrand(user=user, brand=serializers)
,我一直收到以下错误:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rest_framework/views.py", line 466, in dispatch
response = self.handle_exception(exc)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rest_framework/views.py", line 463, in dispatch
response = handler(request, *args, **kwargs)
File "/Users/jeansymolanza/projects/adsoma-api/api/views.py", line 137, in post
return self.create(request, *args, **kwargs)
File "/Users/jeansymolanza/projects/adsoma-api/api/views.py", line 154, in create
user_brand = UserBrand(user=user, brand=serializers)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/base.py", line 439, in __init__
setattr(self, field.name, rel_obj)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 207, in __set__
self.field.remote_field.model._meta.object_name,
ValueError: Cannot assign "<module 'rest_framework.serializers' from '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rest_framework/serializers.py'>": "UserBrand.brand" must be a "Brand" instance.
我想要做的就是将外键(品牌)应用到用户品牌对象中。
views.py
class BrandSignup(generics.CreateAPIView):
"""
Brand signup
HTTP POST
"""
queryset = Brand.objects.all()
serializer_class = BrandSignupSerializer
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
def create(self, request, *args, **kwargs):
if User.objects.filter(email=request.data['email']).exists():
response_details = {
'data': "",
'message': "This email account is already in use. Please try using another one.",
'code': "400",
'status': HTTP_400_BAD_REQUEST
}
return Response(response_details, status=response_details['status'])
else:
serializer = self.get_serializer(data=request.data)
if serializer.is_valid(raise_exception=True):
serializer.save()
user = User(email=request.data['email'], password=request.data['password'],
account_type=request.data['account_type'])
user.save()
user_brand = UserBrand(user=user, brand=serializer)
user_brand.save()
response_details = {
'data': request.data,
'message': "Account created successfully.",
'code': "201",
'status': HTTP_201_CREATED
}
else:
response_details = {
'data': serializer.errors,
'message': "Your account cannot be created at this moment in time. Please try again later.",
'code': "400",
'status': HTTP_400_BAD_REQUEST
}
return Response(response_details, status=response_details['status'])
models.py
class Brand(models.Model):
"""
Brand model
"""
id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
name = models.CharField(max_length=255, null=True, default=None)
brand = models.CharField(max_length=255, null=True, default=None)
phone = models.CharField(max_length=255, null=True, default=None)
website = models.CharField(max_length=255, null=True, default=None)
class Meta:
verbose_name_plural = "Brands"
def __str__(self):
return "%s" % self.brand
class UserBrand(models.Model):
"""
User brand model
"""
id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
user = models.ForeignKey(User, null=False, on_delete=models.CASCADE)
brand = models.ForeignKey(Brand, null=False, on_delete=models.CASCADE)
class Meta:
verbose_name_plural = "User Brands"
def __str__(self):
return "%s - %s" % (self.user, self.brand)
答案 0 :(得分:1)
您正试图将=INDEX(A1:A7,MATCH(MAX(COUNTIF(A1:A7,A1:A7)),COUNTIF(A1:A7,A1:A7),0))
对象作为BrandSignupSerializer
传递给UserBrand
构造函数...您需要一个brand
对象。你应该能够通过用
Brand