0

I getting this error when trying to add first name and last name in my signup froms.

NOT NULL constraint failed: members_subscriber.first_name

console error:

return Database.Cursor.execute(self, query, params) django.db.utils.IntegrityError: NOT NULL constraint failed: members_subscriber.first_name [30/Jul/2021 10:50:44] "POST /subscriber-signup/? HTTP/1.1" 500 193967

getting error after this this two line in my froms.py

 # error occurring for this two line 
            first_name = self.cleaned_data.get('first_name'),
            last_name = self.cleaned_data.get('last_name'),
    

forms.py

class SubscriberSignUpForm(UserCreationForm):
      class Meta(UserCreationForm.Meta):
           model = UserManagement
      

      @transaction.atomic
      def save(self):
        user = super().save(commit=False)
        user.is_subscriber = True
        user.save()
        Subscriber.objects.create(
            user=user,
            email=self.cleaned_data.get('email'),

            # error occurring for this two line 
            first_name = self.cleaned_data.get('first_name'),
            last_name = self.cleaned_data.get('last_name'),
        )
        my_group = Group.objects.get(name='subscriber')
        if user.is_subscriber == True:
            my_group.user_set.add(user) 
        return user

views.py

def form_valid(self, form):
        form.instance.user = self.request.user
        user = form.save()
        login(self.request, user)
        messages.add_message(self.request, messages.INFO,'You Sucessfully Login')
        return redirect('blog:my-account')
boyenec
  • 1,405
  • 5
  • 29
  • Have you checked if the form actually `post`s a `first_name`? – Selcuk Jul 30 '21 at 05:10
  • @Selcuk how to check in my models.py first_name like this `first_name = models.CharField(max_length=200)` – boyenec Jul 30 '21 at 05:12
  • What I meant was to check if the browser actually sends that information. – Selcuk Jul 30 '21 at 05:15
  • as @Selcuk said you can debug your flow to see if browser sends all data correctly. as an example you can use Django debug in VSCode – omid Jul 30 '21 at 05:17
  • @Selcuk I added my VS code console error. I don't know how to do that you are saying. – boyenec Jul 30 '21 at 05:24
  • @Selcuk form is not rendering. when I put `{{form.email}}` it's rendering email fields but when try '{{form.first_name}}` it's not rendering. – boyenec Jul 30 '21 at 05:30
  • 1
    Apparently you don't have such a field in your form. Is this your whole code? `SubscriberSignUpForm` won't have that field unless `UserManagement` model has a field named `first_name` (which I believe doesn't). You should [manually add](https://stackoverflow.com/questions/13550515/django-add-extra-field-to-a-modelform-generated-from-a-model) fields required by the second model (`Subscriber`) to your form. – Selcuk Jul 30 '21 at 05:48
  • @Selcuk you are right. After manually add first_name and last_name in my froms.py it's worked. Usually we don't need to add fields manually to others type of forms but as it user creation forms so it's need to be manually add at begging of our forms class. – boyenec Jul 30 '21 at 07:00

0 Answers0