AttributeError at /accounts/signup/ 'str' object has no attribute 'add'












2















Am new using django so i used practical example on this tutorial on extending user model using User Profile, am having trouble on form.py it gives that error, on removing this line;
supervisor.su_mobile_number.add(*self.cleaned_data.get('su_mobile_number'))
it works smoothly but no data for su_mobile_number was inserted o the database



view.py



class SupervisorSignUpView(CreateView):
model = User
form_class = SupervisorSignUpForm
template_name = 'registration/signup_form.html'

def get_context_data(self, **kwargs):
kwargs['user_type'] = 'supervisor'
return super().get_context_data(**kwargs)

def form_valid(self, form):
user = form.save()
login(self.request, user)
return redirect('home')


model.py



class User(AbstractUser):
is_supervisor = models.BooleanField(default=False)
is_student = models.BooleanField(default=False)


class Supervisor(models.Model):
user = models.OneToOneField('User', on_delete=models.CASCADE, primary_key=True, related_name='supervisor')
su_mobile_number = models.CharField(max_length=200)


forms.py



class SupervisorSignUpForm(UserCreationForm):
su_mobile_number = forms.CharField()

class Meta(UserCreationForm.Meta):
model = User

@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_supervisor = True
user.save()
supervisor = Supervisor.objects.create(user=user)
supervisor.su_mobile_number.add(*self.cleaned_data.get('su_mobile_number'))
return user









share|improve this question

























  • su_mobile_number field is a CharField. What you are trying to do with supervisor.su_mobile_number.add?

    – Abdul Niyas P M
    Dec 31 '18 at 9:32













  • @AbdulNiyasPM, thanks for the reply, i want to save it to db after creating user associated with it

    – japhary juma
    Dec 31 '18 at 9:36






  • 1





    supervisor = Supervisor.objects.create(user=user, su_mobile_number=self.cleaned_data.get('su_mobile_number'))) ??

    – Abdul Niyas P M
    Dec 31 '18 at 9:37
















2















Am new using django so i used practical example on this tutorial on extending user model using User Profile, am having trouble on form.py it gives that error, on removing this line;
supervisor.su_mobile_number.add(*self.cleaned_data.get('su_mobile_number'))
it works smoothly but no data for su_mobile_number was inserted o the database



view.py



class SupervisorSignUpView(CreateView):
model = User
form_class = SupervisorSignUpForm
template_name = 'registration/signup_form.html'

def get_context_data(self, **kwargs):
kwargs['user_type'] = 'supervisor'
return super().get_context_data(**kwargs)

def form_valid(self, form):
user = form.save()
login(self.request, user)
return redirect('home')


model.py



class User(AbstractUser):
is_supervisor = models.BooleanField(default=False)
is_student = models.BooleanField(default=False)


class Supervisor(models.Model):
user = models.OneToOneField('User', on_delete=models.CASCADE, primary_key=True, related_name='supervisor')
su_mobile_number = models.CharField(max_length=200)


forms.py



class SupervisorSignUpForm(UserCreationForm):
su_mobile_number = forms.CharField()

class Meta(UserCreationForm.Meta):
model = User

@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_supervisor = True
user.save()
supervisor = Supervisor.objects.create(user=user)
supervisor.su_mobile_number.add(*self.cleaned_data.get('su_mobile_number'))
return user









share|improve this question

























  • su_mobile_number field is a CharField. What you are trying to do with supervisor.su_mobile_number.add?

    – Abdul Niyas P M
    Dec 31 '18 at 9:32













  • @AbdulNiyasPM, thanks for the reply, i want to save it to db after creating user associated with it

    – japhary juma
    Dec 31 '18 at 9:36






  • 1





    supervisor = Supervisor.objects.create(user=user, su_mobile_number=self.cleaned_data.get('su_mobile_number'))) ??

    – Abdul Niyas P M
    Dec 31 '18 at 9:37














2












2








2








Am new using django so i used practical example on this tutorial on extending user model using User Profile, am having trouble on form.py it gives that error, on removing this line;
supervisor.su_mobile_number.add(*self.cleaned_data.get('su_mobile_number'))
it works smoothly but no data for su_mobile_number was inserted o the database



view.py



class SupervisorSignUpView(CreateView):
model = User
form_class = SupervisorSignUpForm
template_name = 'registration/signup_form.html'

def get_context_data(self, **kwargs):
kwargs['user_type'] = 'supervisor'
return super().get_context_data(**kwargs)

def form_valid(self, form):
user = form.save()
login(self.request, user)
return redirect('home')


model.py



class User(AbstractUser):
is_supervisor = models.BooleanField(default=False)
is_student = models.BooleanField(default=False)


class Supervisor(models.Model):
user = models.OneToOneField('User', on_delete=models.CASCADE, primary_key=True, related_name='supervisor')
su_mobile_number = models.CharField(max_length=200)


forms.py



class SupervisorSignUpForm(UserCreationForm):
su_mobile_number = forms.CharField()

class Meta(UserCreationForm.Meta):
model = User

@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_supervisor = True
user.save()
supervisor = Supervisor.objects.create(user=user)
supervisor.su_mobile_number.add(*self.cleaned_data.get('su_mobile_number'))
return user









share|improve this question
















Am new using django so i used practical example on this tutorial on extending user model using User Profile, am having trouble on form.py it gives that error, on removing this line;
supervisor.su_mobile_number.add(*self.cleaned_data.get('su_mobile_number'))
it works smoothly but no data for su_mobile_number was inserted o the database



view.py



class SupervisorSignUpView(CreateView):
model = User
form_class = SupervisorSignUpForm
template_name = 'registration/signup_form.html'

def get_context_data(self, **kwargs):
kwargs['user_type'] = 'supervisor'
return super().get_context_data(**kwargs)

def form_valid(self, form):
user = form.save()
login(self.request, user)
return redirect('home')


model.py



class User(AbstractUser):
is_supervisor = models.BooleanField(default=False)
is_student = models.BooleanField(default=False)


class Supervisor(models.Model):
user = models.OneToOneField('User', on_delete=models.CASCADE, primary_key=True, related_name='supervisor')
su_mobile_number = models.CharField(max_length=200)


forms.py



class SupervisorSignUpForm(UserCreationForm):
su_mobile_number = forms.CharField()

class Meta(UserCreationForm.Meta):
model = User

@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_supervisor = True
user.save()
supervisor = Supervisor.objects.create(user=user)
supervisor.su_mobile_number.add(*self.cleaned_data.get('su_mobile_number'))
return user






python django






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 9:33







japhary juma

















asked Dec 31 '18 at 9:30









japhary jumajaphary juma

3210




3210













  • su_mobile_number field is a CharField. What you are trying to do with supervisor.su_mobile_number.add?

    – Abdul Niyas P M
    Dec 31 '18 at 9:32













  • @AbdulNiyasPM, thanks for the reply, i want to save it to db after creating user associated with it

    – japhary juma
    Dec 31 '18 at 9:36






  • 1





    supervisor = Supervisor.objects.create(user=user, su_mobile_number=self.cleaned_data.get('su_mobile_number'))) ??

    – Abdul Niyas P M
    Dec 31 '18 at 9:37



















  • su_mobile_number field is a CharField. What you are trying to do with supervisor.su_mobile_number.add?

    – Abdul Niyas P M
    Dec 31 '18 at 9:32













  • @AbdulNiyasPM, thanks for the reply, i want to save it to db after creating user associated with it

    – japhary juma
    Dec 31 '18 at 9:36






  • 1





    supervisor = Supervisor.objects.create(user=user, su_mobile_number=self.cleaned_data.get('su_mobile_number'))) ??

    – Abdul Niyas P M
    Dec 31 '18 at 9:37

















su_mobile_number field is a CharField. What you are trying to do with supervisor.su_mobile_number.add?

– Abdul Niyas P M
Dec 31 '18 at 9:32







su_mobile_number field is a CharField. What you are trying to do with supervisor.su_mobile_number.add?

– Abdul Niyas P M
Dec 31 '18 at 9:32















@AbdulNiyasPM, thanks for the reply, i want to save it to db after creating user associated with it

– japhary juma
Dec 31 '18 at 9:36





@AbdulNiyasPM, thanks for the reply, i want to save it to db after creating user associated with it

– japhary juma
Dec 31 '18 at 9:36




1




1





supervisor = Supervisor.objects.create(user=user, su_mobile_number=self.cleaned_data.get('su_mobile_number'))) ??

– Abdul Niyas P M
Dec 31 '18 at 9:37





supervisor = Supervisor.objects.create(user=user, su_mobile_number=self.cleaned_data.get('su_mobile_number'))) ??

– Abdul Niyas P M
Dec 31 '18 at 9:37












1 Answer
1






active

oldest

votes


















2














Use = operator to assign value and then call save() method



@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_supervisor = True
user.save()
supervisor = Supervisor.objects.create(user=user)
supervisor.su_mobile_number = self.cleaned_data.get('su_mobile_number')
supervisor.save()

return user




more shorter way is (as @Abdul Niyas P M mentioned)



@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_supervisor = True
user.save()
supervisor = Supervisor.objects.create(user=user, su_mobile_number=self.cleaned_data.get('su_mobile_number'))
return user





share|improve this answer
























  • let me try it, i will getb back to you, thanks

    – japhary juma
    Dec 31 '18 at 9:45













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%2f53985793%2fattributeerror-at-accounts-signup-str-object-has-no-attribute-add%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Use = operator to assign value and then call save() method



@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_supervisor = True
user.save()
supervisor = Supervisor.objects.create(user=user)
supervisor.su_mobile_number = self.cleaned_data.get('su_mobile_number')
supervisor.save()

return user




more shorter way is (as @Abdul Niyas P M mentioned)



@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_supervisor = True
user.save()
supervisor = Supervisor.objects.create(user=user, su_mobile_number=self.cleaned_data.get('su_mobile_number'))
return user





share|improve this answer
























  • let me try it, i will getb back to you, thanks

    – japhary juma
    Dec 31 '18 at 9:45


















2














Use = operator to assign value and then call save() method



@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_supervisor = True
user.save()
supervisor = Supervisor.objects.create(user=user)
supervisor.su_mobile_number = self.cleaned_data.get('su_mobile_number')
supervisor.save()

return user




more shorter way is (as @Abdul Niyas P M mentioned)



@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_supervisor = True
user.save()
supervisor = Supervisor.objects.create(user=user, su_mobile_number=self.cleaned_data.get('su_mobile_number'))
return user





share|improve this answer
























  • let me try it, i will getb back to you, thanks

    – japhary juma
    Dec 31 '18 at 9:45
















2












2








2







Use = operator to assign value and then call save() method



@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_supervisor = True
user.save()
supervisor = Supervisor.objects.create(user=user)
supervisor.su_mobile_number = self.cleaned_data.get('su_mobile_number')
supervisor.save()

return user




more shorter way is (as @Abdul Niyas P M mentioned)



@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_supervisor = True
user.save()
supervisor = Supervisor.objects.create(user=user, su_mobile_number=self.cleaned_data.get('su_mobile_number'))
return user





share|improve this answer













Use = operator to assign value and then call save() method



@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_supervisor = True
user.save()
supervisor = Supervisor.objects.create(user=user)
supervisor.su_mobile_number = self.cleaned_data.get('su_mobile_number')
supervisor.save()

return user




more shorter way is (as @Abdul Niyas P M mentioned)



@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_supervisor = True
user.save()
supervisor = Supervisor.objects.create(user=user, su_mobile_number=self.cleaned_data.get('su_mobile_number'))
return user






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 31 '18 at 9:38









JPGJPG

13.7k2832




13.7k2832













  • let me try it, i will getb back to you, thanks

    – japhary juma
    Dec 31 '18 at 9:45





















  • let me try it, i will getb back to you, thanks

    – japhary juma
    Dec 31 '18 at 9:45



















let me try it, i will getb back to you, thanks

– japhary juma
Dec 31 '18 at 9:45







let me try it, i will getb back to you, thanks

– japhary juma
Dec 31 '18 at 9:45




















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%2f53985793%2fattributeerror-at-accounts-signup-str-object-has-no-attribute-add%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

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas