Sunday, May 3, 2015

Re: Creating User object with is_active = False

The line that's failing is not for the User model, but for the BeeradUser model.

Assuming this is your same model


Then I guess you have a "is_active" field in the BeeradUser model that also has to be set to False when you create the model instance.

On Sun, May 3, 2015 at 11:37 PM, bradford li <bradfordli@gmail.com> wrote:
I posted the same question on stackoverflow if anyone wants to answer it there.



I am trying to create a builtin User object that has `is_active = False` but I am getting this error:

null value in column "is_active" violates not-null constraint


Traceback:

    File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
      111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "/Users/bli1/Development/Django/beerad/userapp/views.py" in register
      30.             beerad_user.save()
    File "/Library/Python/2.7/site-packages/django/db/models/base.py" in save
      589.                        force_update=force_update, update_fields=update_fields)
    File "/Library/Python/2.7/site-packages/django/db/models/base.py" in save_base
      617.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
    File "/Library/Python/2.7/site-packages/django/db/models/base.py" in _save_table
      698.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
    File "/Library/Python/2.7/site-packages/django/db/models/base.py" in _do_insert
      731.                                using=using, raw=raw)
    File "/Library/Python/2.7/site-packages/django/db/models/manager.py" in manager_method
      92.                 return getattr(self.get_queryset(), name)(*args, **kwargs)
    File "/Library/Python/2.7/site-packages/django/db/models/query.py" in _insert
      921.         return query.get_compiler(using=using).execute_sql(return_id)
    File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
      920.                 cursor.execute(sql, params)
    File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
      81.             return super(CursorDebugWrapper, self).execute(sql, params)
    File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
      65.                 return self.cursor.execute(sql, params)
    File "/Library/Python/2.7/site-packages/django/db/utils.py" in __exit__
      94.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
    File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
      65.                 return self.cursor.execute(sql, params)
    
    Exception Type: IntegrityError at /auth/signup/
    Exception Value: null value in column "is_active" violates not-null constraint
    DETAIL:  Failing row contains (9, null, 52eed3a4-5232-4819-af74-d534ca6cc3a7, 12).


Code:

    def register(request):
        if request.method == "POST":
            registration_form = RegistrationForm(request.POST)
            if registration_form.is_valid():
                # create new user
                form_data = registration_form.cleaned_data
                print(form_data)
                first_name = form_data["first_name"]
                last_name = form_data["last_name"]
                username = form_data["username"]
                email = form_data["email"]
                password = form_data["password"]
                new_user = User.objects.create(username=username, first_name=first_name, last_name=last_name, email=email, password=password, is_active=False)
                unique_key = str(uuid.uuid4())
                beerad_user = BeeradUser(user=new_user, activation_key=unique_key)
                beerad_user.save()

I also tried creating the User object and then using this line of code:

new_user.is_active = False

But I get the error. Without adding the `is_active` property, creating a new User works but it sets the `is_active` field to be automatically True which isn't something I want.

Here is my BeeradUser model:

    class BeeradUser(models.Model):
        user = models.OneToOneField(User)
        activation_key = models.CharField(max_length=36)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7a11e83f-cf4b-4ea5-b9d2-1765cb6a25c2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALn3ei1LAO_0gfhK8MaBYgV_ehW2Q4y427eZAwE4YGNkDcCZVQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment