4

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')
Moe Far
  • 2,742
  • 2
  • 23
  • 41
Yunti
  • 6,761
  • 12
  • 61
  • 106

2 Answers2

10

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):
    ...
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • I'm trying to temporarily save the form data to an anonymous user. There are a couple of pages of forms which I would like to save to the database with an anonymous user. That way a new user to the website can come along go through the first couple of forms - check our service and if they decided not to sign up afterwards I can dump the data from the database (or keep it attached to a session id). I don't want to force users to have to sign up first. I thought the Session ID would essentially be saved to database as the anonymous user? Does the anonymous user = session ID – Yunti Feb 08 '16 at 14:36
  • An anonymous user doesn't have a primary key or any persistence. You can't identify it by session key or any other identifier. If you want to store the session id, you'll have to add an extra field to your model, then make sure that the user field is populated when the user is registered. Another approach could be to create a user automatically, but don't set it active until the user registers. That has its own problems (e.g. what do you set the email address and username of the inactive user to?). – Alasdair Feb 08 '16 at 14:55
  • Ok I think that's my confusion. I thought an anonymous user was essentially the session id and that if a user wasn't logged in the anonymous user was identified by the session ID. What a user ajtl? – Yunti Feb 08 '16 at 14:58
  • Which approach is best practice? I presume this problem is fairly common or (do sites just temporarily store all the data in the session dict rather than database models?) – Yunti Feb 08 '16 at 15:05
  • Storing data in the session is another option, but the disadvantage is that the code for anonymous and logged in users will be quite different, and it will be harder to query the data for anonymous users. I'm not sure what the best practice is. – Alasdair Feb 08 '16 at 15:10
1

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
        ...
luc
  • 41,928
  • 25
  • 127
  • 172