Home Ask Login Register

Developers Planet

Your answer is one click away!

Yunti February 2016

Anonymous user error

I'm trying to save form data to an anonymous user, however I get the below error when trying to save some form data in a CreateView". I'm not clear what the issue is?

ValueError: Cannot assign "<SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x11126bc18>>": "EUser.user" must be a "User" instance.


class EUser(models.Model):
    online_account = models.BooleanField()

    supplier1 = models.OneToOneField(SupplierAccount)
    supplier2 = models.OneToOneField(SupplierAccount)
    supplier3 = models.OneToOneField(SupplierAccount)

    address = models.OneToOneField(Address)
    user = models.ForeignKey(settings.AUTH_USER_MODEL)

class SupplierAccount(models.Model):
    supplier = models.ForeignKey(Supplier)
    username = models.CharField(max_length=255)
    password = models.CharField(max_length=255)


class ServiceTypeForm(forms.ModelForm):
    # BOOL_CHOICES = ((False, 'No'), (True, 'Yes'))

    # online_account = forms.BooleanField(widget=forms.RadioSelect(choices=BOOL_CHOICES))

    def __init__(self, *args, **kwargs):
        super(ServiceTypeForm, self).__init__(*args, **kwargs)
        self.fields['service_type'].initial = 'D'

    class Meta:
        model = EUser
        fields = ('service_type', 'online_account')


class ServiceTypeView(CreateView):
    form_class = ServiceTypeForm
    template_name = "standard_form.html"
    success_url = '/'

    def form_valid(self, form):
        form.instance.user = self.request.user
        online_account = form.cleaned_data['online_account']
        if online_account:
            return redirect('../online')
            return redirect('../address')


Alasdair February 2016

If the user is not logged in, then request.user is an anonymous user. It doesn't make sense to assign an anonymous user to form.instance.user, because an anonymous user does not exist in the database or have a primary key.

How you change your code depends on how you want your application to work.

If you want to allow anonymous users to create service types, then

if self.request.use.is_authenticated():
    form.instance.user = self.request.user

For this to work, you would need to change the user field to make it optional.

user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True)

After making this change, you'll need to run makemigrations and then migrate, to update the database.

Another option would be to restrict the view to logged in users. In Django 1.9+, You can do this with the LoginRequiredMixin.

from django.contrib.auth.mixins import LoginRequiredMixin

class ServiceTypeView(LoginRequiredMixin, CreateView):

luc February 2016

I think you can not use the AnonymousUser as value for a ForeignKey to a User.

You should keep is as Null in this case.

class EUser(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, default=None) 

class ServiceTypeView(CreateView):

    def form_valid(self, form):
        if self.request.user.is_authenticated():
            form.instance.user = self.request.user

Post Status

Asked in February 2016
Viewed 2,622 times
Voted 14
Answered 2 times


Leave an answer

Quote of the day: live life