how to create a model whenever a user is added to specific group in django





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















in django User and i have 2 groups, i want whenever i create a user, create a corresponding profile for it (depending on which group it belongs to) how should i manage it?



i have used this method:



def create_profile(sender, **kwargs):
if kwargs['created'] :
UserProfile.objects.create(user=kwargs['instance'])

post_save.connect(create_profile, sender=User)


But it creates userprofile whenever i create a user.
How can I have conditions to check which group it belongs too and then have create corresponding profile for it



In my registration process i made sure they have a group and it cant be changed



    if 'signup_provider_btn' in request.POST:
providers = Group.objects.get(name='providers')
providers.user_set.add(user)
elif 'signup_user_btn' in request.POST:
users = Group.objects.get(name='users')
users.user_set.add(user)


creating a user and assigning it to a group:



    user = User()
username = request.POST.get('username', '')
user.username = username
password = request.POST.get('password', '')
user.set_password(password)
# users.name = request.POST.get("")
# users.image = request.POST.get("phone")
user.save()
if 'signup_provider_btn' in request.POST:
providers = Group.objects.get(name='providers')
providers.user_set.add(user)
elif 'signup_user_btn' in request.POST:
users = Group.objects.get(name='users')
users.user_set.add(user)









share|improve this question

























  • What are you going to do if someone changes the user's group ? What if the user is created with more than one group ? Or with no group at all ? Also, the user record is created before it's added to any group, so you won't never get this information at this stage (I mean, in a post_save handler called with created=True).

    – bruno desthuilliers
    Jan 3 at 16:14











  • I edited my question, u can see they have group and is just one of them and there is no way to change it

    – arian
    Jan 3 at 16:23











  • the fact you're doing this in your registration process doesn't garantee a user will have a group and a single one - a user can be created outside your registration process - nor that there's no way it's group(s) won't ever change (this can be done in the admin or by just any part of the code). And from experience, I can garantee you will have to deal with those cases at one point or another (unless your project is abandonned before being ever used at least).

    – bruno desthuilliers
    Jan 3 at 16:29











  • As a side note: the above excerpt of your "registration process" code contains the very obious answer to your question - create the profile at this point, which is where you do have all the relevant informations ;-)

    – bruno desthuilliers
    Jan 3 at 16:30











  • yeah i figured it out after i asked it but i wanted to make use of signal and sender thank you by the way

    – arian
    Jan 3 at 17:22


















0















in django User and i have 2 groups, i want whenever i create a user, create a corresponding profile for it (depending on which group it belongs to) how should i manage it?



i have used this method:



def create_profile(sender, **kwargs):
if kwargs['created'] :
UserProfile.objects.create(user=kwargs['instance'])

post_save.connect(create_profile, sender=User)


But it creates userprofile whenever i create a user.
How can I have conditions to check which group it belongs too and then have create corresponding profile for it



In my registration process i made sure they have a group and it cant be changed



    if 'signup_provider_btn' in request.POST:
providers = Group.objects.get(name='providers')
providers.user_set.add(user)
elif 'signup_user_btn' in request.POST:
users = Group.objects.get(name='users')
users.user_set.add(user)


creating a user and assigning it to a group:



    user = User()
username = request.POST.get('username', '')
user.username = username
password = request.POST.get('password', '')
user.set_password(password)
# users.name = request.POST.get("")
# users.image = request.POST.get("phone")
user.save()
if 'signup_provider_btn' in request.POST:
providers = Group.objects.get(name='providers')
providers.user_set.add(user)
elif 'signup_user_btn' in request.POST:
users = Group.objects.get(name='users')
users.user_set.add(user)









share|improve this question

























  • What are you going to do if someone changes the user's group ? What if the user is created with more than one group ? Or with no group at all ? Also, the user record is created before it's added to any group, so you won't never get this information at this stage (I mean, in a post_save handler called with created=True).

    – bruno desthuilliers
    Jan 3 at 16:14











  • I edited my question, u can see they have group and is just one of them and there is no way to change it

    – arian
    Jan 3 at 16:23











  • the fact you're doing this in your registration process doesn't garantee a user will have a group and a single one - a user can be created outside your registration process - nor that there's no way it's group(s) won't ever change (this can be done in the admin or by just any part of the code). And from experience, I can garantee you will have to deal with those cases at one point or another (unless your project is abandonned before being ever used at least).

    – bruno desthuilliers
    Jan 3 at 16:29











  • As a side note: the above excerpt of your "registration process" code contains the very obious answer to your question - create the profile at this point, which is where you do have all the relevant informations ;-)

    – bruno desthuilliers
    Jan 3 at 16:30











  • yeah i figured it out after i asked it but i wanted to make use of signal and sender thank you by the way

    – arian
    Jan 3 at 17:22














0












0








0








in django User and i have 2 groups, i want whenever i create a user, create a corresponding profile for it (depending on which group it belongs to) how should i manage it?



i have used this method:



def create_profile(sender, **kwargs):
if kwargs['created'] :
UserProfile.objects.create(user=kwargs['instance'])

post_save.connect(create_profile, sender=User)


But it creates userprofile whenever i create a user.
How can I have conditions to check which group it belongs too and then have create corresponding profile for it



In my registration process i made sure they have a group and it cant be changed



    if 'signup_provider_btn' in request.POST:
providers = Group.objects.get(name='providers')
providers.user_set.add(user)
elif 'signup_user_btn' in request.POST:
users = Group.objects.get(name='users')
users.user_set.add(user)


creating a user and assigning it to a group:



    user = User()
username = request.POST.get('username', '')
user.username = username
password = request.POST.get('password', '')
user.set_password(password)
# users.name = request.POST.get("")
# users.image = request.POST.get("phone")
user.save()
if 'signup_provider_btn' in request.POST:
providers = Group.objects.get(name='providers')
providers.user_set.add(user)
elif 'signup_user_btn' in request.POST:
users = Group.objects.get(name='users')
users.user_set.add(user)









share|improve this question
















in django User and i have 2 groups, i want whenever i create a user, create a corresponding profile for it (depending on which group it belongs to) how should i manage it?



i have used this method:



def create_profile(sender, **kwargs):
if kwargs['created'] :
UserProfile.objects.create(user=kwargs['instance'])

post_save.connect(create_profile, sender=User)


But it creates userprofile whenever i create a user.
How can I have conditions to check which group it belongs too and then have create corresponding profile for it



In my registration process i made sure they have a group and it cant be changed



    if 'signup_provider_btn' in request.POST:
providers = Group.objects.get(name='providers')
providers.user_set.add(user)
elif 'signup_user_btn' in request.POST:
users = Group.objects.get(name='users')
users.user_set.add(user)


creating a user and assigning it to a group:



    user = User()
username = request.POST.get('username', '')
user.username = username
password = request.POST.get('password', '')
user.set_password(password)
# users.name = request.POST.get("")
# users.image = request.POST.get("phone")
user.save()
if 'signup_provider_btn' in request.POST:
providers = Group.objects.get(name='providers')
providers.user_set.add(user)
elif 'signup_user_btn' in request.POST:
users = Group.objects.get(name='users')
users.user_set.add(user)






django python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 17:28







arian

















asked Jan 3 at 15:39









arianarian

61




61













  • What are you going to do if someone changes the user's group ? What if the user is created with more than one group ? Or with no group at all ? Also, the user record is created before it's added to any group, so you won't never get this information at this stage (I mean, in a post_save handler called with created=True).

    – bruno desthuilliers
    Jan 3 at 16:14











  • I edited my question, u can see they have group and is just one of them and there is no way to change it

    – arian
    Jan 3 at 16:23











  • the fact you're doing this in your registration process doesn't garantee a user will have a group and a single one - a user can be created outside your registration process - nor that there's no way it's group(s) won't ever change (this can be done in the admin or by just any part of the code). And from experience, I can garantee you will have to deal with those cases at one point or another (unless your project is abandonned before being ever used at least).

    – bruno desthuilliers
    Jan 3 at 16:29











  • As a side note: the above excerpt of your "registration process" code contains the very obious answer to your question - create the profile at this point, which is where you do have all the relevant informations ;-)

    – bruno desthuilliers
    Jan 3 at 16:30











  • yeah i figured it out after i asked it but i wanted to make use of signal and sender thank you by the way

    – arian
    Jan 3 at 17:22



















  • What are you going to do if someone changes the user's group ? What if the user is created with more than one group ? Or with no group at all ? Also, the user record is created before it's added to any group, so you won't never get this information at this stage (I mean, in a post_save handler called with created=True).

    – bruno desthuilliers
    Jan 3 at 16:14











  • I edited my question, u can see they have group and is just one of them and there is no way to change it

    – arian
    Jan 3 at 16:23











  • the fact you're doing this in your registration process doesn't garantee a user will have a group and a single one - a user can be created outside your registration process - nor that there's no way it's group(s) won't ever change (this can be done in the admin or by just any part of the code). And from experience, I can garantee you will have to deal with those cases at one point or another (unless your project is abandonned before being ever used at least).

    – bruno desthuilliers
    Jan 3 at 16:29











  • As a side note: the above excerpt of your "registration process" code contains the very obious answer to your question - create the profile at this point, which is where you do have all the relevant informations ;-)

    – bruno desthuilliers
    Jan 3 at 16:30











  • yeah i figured it out after i asked it but i wanted to make use of signal and sender thank you by the way

    – arian
    Jan 3 at 17:22

















What are you going to do if someone changes the user's group ? What if the user is created with more than one group ? Or with no group at all ? Also, the user record is created before it's added to any group, so you won't never get this information at this stage (I mean, in a post_save handler called with created=True).

– bruno desthuilliers
Jan 3 at 16:14





What are you going to do if someone changes the user's group ? What if the user is created with more than one group ? Or with no group at all ? Also, the user record is created before it's added to any group, so you won't never get this information at this stage (I mean, in a post_save handler called with created=True).

– bruno desthuilliers
Jan 3 at 16:14













I edited my question, u can see they have group and is just one of them and there is no way to change it

– arian
Jan 3 at 16:23





I edited my question, u can see they have group and is just one of them and there is no way to change it

– arian
Jan 3 at 16:23













the fact you're doing this in your registration process doesn't garantee a user will have a group and a single one - a user can be created outside your registration process - nor that there's no way it's group(s) won't ever change (this can be done in the admin or by just any part of the code). And from experience, I can garantee you will have to deal with those cases at one point or another (unless your project is abandonned before being ever used at least).

– bruno desthuilliers
Jan 3 at 16:29





the fact you're doing this in your registration process doesn't garantee a user will have a group and a single one - a user can be created outside your registration process - nor that there's no way it's group(s) won't ever change (this can be done in the admin or by just any part of the code). And from experience, I can garantee you will have to deal with those cases at one point or another (unless your project is abandonned before being ever used at least).

– bruno desthuilliers
Jan 3 at 16:29













As a side note: the above excerpt of your "registration process" code contains the very obious answer to your question - create the profile at this point, which is where you do have all the relevant informations ;-)

– bruno desthuilliers
Jan 3 at 16:30





As a side note: the above excerpt of your "registration process" code contains the very obious answer to your question - create the profile at this point, which is where you do have all the relevant informations ;-)

– bruno desthuilliers
Jan 3 at 16:30













yeah i figured it out after i asked it but i wanted to make use of signal and sender thank you by the way

– arian
Jan 3 at 17:22





yeah i figured it out after i asked it but i wanted to make use of signal and sender thank you by the way

– arian
Jan 3 at 17:22












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025447%2fhow-to-create-a-model-whenever-a-user-is-added-to-specific-group-in-django%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025447%2fhow-to-create-a-model-whenever-a-user-is-added-to-specific-group-in-django%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]