def artist(request):
print("START")
if request.method == "GET":
try:
artist = Artist.objects.get(user=request.user)
print("artist exists")
except Artist.DoesNotExist:
print("artist doesn't exist")
artist = Artist.objects.get_or_create(user=request.user)[0]
print("a new artist object has been created")
return HttpResponse(status=200)
Console:
START
artist doesn't exist
START
artist doesn't exist
a new artist object has been created
[15/Dec/2022 12:40:23] "GET /artist/ HTTP/1.1" 200 0
a new artist object has been created
[15/Dec/2022 12:40:23] "GET /artist/ HTTP/1.1" 200 0
on the client side, there is a button on the screen that sends a request that returns to the user their artist page, or if they don't have one, it will be created for them. And suppose he double clicks the button, then two objects will be created. The user
field in the Artist
is not unique, we ignore the fact that we can solve the problem on the client side with javascript and we assume that the server is a bit slow, so it doesn't have time to process two requests in such a short time.
The Artist
model has a field that at the time of creation must be populated with some information based on their profile picture, i.e. i extract the dominant color from their picture to use it later on the page, it's irrelevant, we just assume it takes a bit longer to create
I also want to create the object without a form, I will use a form later to update the profile if the user wants. Also ignore creating the Artist
object once the user
is created ( using the signal thing)