Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Models:

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)

Form:

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')

View:

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

    def form_valid(self, form):
        form.instance.user = self.request.user
        super().form_valid(form)
        online_account = form.cleaned_data['online_account']
        if online_account:
            return redirect('../online')
        else:
            return redirect('../address')
like image 394
Yunti Avatar asked Feb 07 '23 19:02

Yunti


2 Answers

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.user.is_authenticated(): # Django < 1.10 
if self.request.user.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):
    ...
like image 100
Alasdair Avatar answered Feb 16 '23 02:02

Alasdair


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
        ...
like image 37
luc Avatar answered Feb 16 '23 02:02

luc