#Create Artist instance when a User instance gets saved.

45 messages · Page 1 of 1 (latest)

karmic hornet
#

Hi I am writing a REST API. In my accounts.models, I have a Custom User model inheriting from the AbstractBaseUser model and contains fields like username, email, first_name, last_name and is_artist.

I also have a profiles.models which has an Artist class and contains additional fields.

I want a situation whereby once a User is created, an Artist instance is also created. But only when the user set the is_artist to True should the Artist update be made available to them so they can update their Artist profile. How can I achieve this?

formal frigate
#

Am I correct that you should create a new artist instance if is_artist is true? What about using signal or override perform_create to create a new artist instance if is_artist is true on signup?

karmic hornet
#

And the authentication endpoints are handled by Djoser.

formal frigate
karmic hornet
#

I just wrote this:

@receiver(post_save, sender=AUTH_USER_MODEL)
def create_artist_profile(sender, instance, created, **kwargs):
    if created:
        Artist.objects.create(user=instance)
        logger.info(f"{instance}'s profile created")

Have I got it right? Do I need to modify my managers.py ?

formal frigate
karmic hornet
#

I am getting the error, "Unable to create account"

formal frigate
#

Is the problem occurring in the Artist.objects.create() code (I guess you could remove create() below the if statement and try print(instance) or something. ) I'm not sure what logic Djoser uses to handle signups, but if the error you're experiencing is an exception thrown by python and not handled by djoser, I'd be curious to see what traceback looks like.

#

I think people who are having this problem are usually getting the above error because of REQUIRED_FIELDS. Since you're using a custom user model, you might want to check that out.

karmic hornet
#

Yes, I am able to create accounts without signals.

#

But once I include signals, everything goes back again.

formal frigate
karmic hornet
#

Sorry, I am not very familiar with the breakpoint thing. How do you do it?

formal frigate
#

What IDE are you using?

karmic hornet
#

VScode...

#

It would be really helpful to look at my repo. Been battling this for a while now.

formal frigate
karmic hornet
#

Ah yes, it is hosted by github. Thank you very much.

karmic hornet
#

@formal frigate Any headway yet?

formal frigate
#

@karmic hornet just made some PR, check it out.

#

Did it work?

karmic hornet
formal frigate
#

Oh I omitted is_artist logic, you can customize the signal code..

karmic hornet
#

Okay. Let me include this then..
Though it's giving me this error now: django.db.utils.IntegrityError: duplicate key value violates unique constraint "profiles_artist_stage_name_key"
DETAIL: Key (stage_name)=() already exists.

#

And do I pass in "is_artist" as one of the arguments in the my_handler function?

formal frigate
#
def my_handler(user, request, **kwargs):
    logger.info(f"{user} has registered")
    artist = Artist.objects.create(user=user)
    logger.info(f"{artist} has been created")

"user" means created user instance, so you can make a logic like below (not tested..)

if user.is_artist:
    logger.info("this new user is artist!")
    artist = Artist.objects.create(user=user)
    logger.info(f"{artist} has been created")
else:
    pass
karmic hornet
karmic hornet
# formal frigate happy coding bro XD

There's a problem. When I cleared my DB and tried to create a new user, it was created successfully. But when I tried to create another user, it gave me the same error as before.

formal frigate
#

can you try objcets.update_or_create instead of objects.create ? still don't know why it happens..

karmic hornet
formal frigate
# karmic hornet Okay. Let me try that right away...

i just saw your model.

stage_name = models.CharField(verbose_name=_("Stage Name"), max_length=255, unique=True)

in this signal code,

artist = Artist.objects.create(user=user)

it makes new artist instnace that stage_name is empty value..(stage_name="")

the stage_name field declared as unique, So I think DB logic is violating the uniqueness constraint on the second creation attempt.

Is there a reason why stage_name should be unique? Or how about requiring the stage_name to be included when signing up?

karmic hornet
formal frigate
#

User objects have username, right?
so you can try this...

if user.is_artist:
    logger.info("this new user is artist!")
    artist = Artist.objects.create(user=user,
    # stage_name = user.username
    )
    logger.info(f"{artist} has been created")
else:
    pass
karmic hornet
#

What I expected was that it would create an instance of the Artist when a user class is created. And then there would be the option of the artist to update their Artist profile much later whenever they wish.

#

And yes, I am going to define the logic for the Artist profile update.

#

Means it's going to have a different endpoint that is not handled by Djoser.

formal frigate
#

The artist instance is actually created. However, the big problem is that you can't inject a cell phone number as the unique constraint when creating an artist instance.

The stage_name can be passed as a username, but the phonenumber can't, right? Whether it's the stage_name field or the phonenumber field, the common problem is that they are both uniquely constrained.

If it were me, I'd either leave the phonenumber field defaulting to an empty value, or move the phonenumber field to the user model.

karmic hornet
#

Let me do that and then try again.

formal frigate
#

lets crash the error message.. 😡