@api_view(["POST"])
def register_view(request: "Request"):
serializer = RegisterSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
try:
serializer.save()
except IntegrityError:
raise ConflictException
return Response(serializer.data, status=status.HTTP_201_CREATED)
class ConflictException(APIException):
status_code = 409
default_detail = "username is already taken"
default_code = "conflict"
def test_username_taken(self):
with transaction.atomic():
User.objects.create_user(username="customer4", password="Passw0rd!")
response = self.client.post(
"/register",
{
"username": "customer4",
"password": "Passw0rd!",
"confirm_password": "Passw0rd!",
"phone_number": "13324252666"
},
)
self.assertEqual(response.status_code, status.HTTP_409_CONFLICT)
The test result shows :
raise TransactionManagementError(
django.db.transaction.TransactionManagementError: An error occurred in the current transaction. You can’t execute queries until the end of the ‘atomic’ block.
I’m a Django and DRF noob and don’t know what to do…
I tested on postman and there is no problem, with 409 status_code response
New contributor
noob is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.