# Unable to assign a user to a Form field through request.user

**URL:** https://forum.djangoproject.com/t/unable-to-assign-a-user-to-a-form-field-through-request-user/18459
**Category:** Forms & APIs
**Created:** [January 26, 2023, 5:54pm UTC](https://forum.djangoproject.com/t/unable-to-assign-a-user-to-a-form-field-through-request-user/18459 "2023-01-26T17:54:58Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Eadwulf](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/eadwulf/32/11910_2.png) [@Eadwulf](https://forum.djangoproject.com/u/Eadwulf)
#### Post date: [January 26, 2023, 5:54pm UTC](https://forum.djangoproject.com/t/unable-to-assign-a-user-to-a-form-field-through-request-user/18459/1 "2023-01-26T17:54:58Z")

</div>

**I want to pass the logged user to a Django Form so it can be saved on submit (request.POST)**

**Let’s start by getting some context.**

I’m building a password manager website. In my Django project, I have three apps:

- Accounts **(has User Model)**
- Credentials **(has Credential Model)**
- Websites **(has Website Model)**  
  

**Accounts Model:**

```auto
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    def __str__ (self):
        return self.username

```

  

**Credential Model:**

```auto
from django.db import models
from django.urls import reverse

from websites.models import Website

class Credential(models.Model):
    username = models.CharField(max_length=32)
    password = models.CharField(max_length=64)
    added_on = models.DateTimeField(auto_now=True)
    website = models.ForeignKey(Website, on_delete=models.CASCADE, related_name='credentials')

    def get_absolute_url(self):
        return reverse('credential_detail', kwargs={'pk': self.pk})

    def __str__ (self):
        return f'{self.username} credential'

```

  

**Website Model:**

```auto
from django.db import models
from django.urls import reverse

from accounts.models import User

class Website(models.Model):
    url = models.URLField(max_length=64)
    name = models.CharField(max_length=64, blank=True, null=True)
    description = models.CharField(max_length=256, blank=True, null=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='websites')

    def get_absolute_url(self):
        return reverse('website_detail', kwargs={'pk': self.pk})
    
    def __str__ (self):
        return self.name

```

  

**The defined relationships are:**

- Many-to-one (Credential - Website)
- Many-to-one (Website - User)  
  

**Relevant Form**

```auto
from django import forms

from accounts.models import User

class WebsiteCreateForm(forms.Form):
    url = forms.URLField(max_length=64)
    name = forms.CharField(max_length=64)
    description = forms.CharField(max_length=256)
    user = forms.ModelChoiceField(queryset=None)

    def __init__ (self, *args, user=None, **kwargs):
        self.user.queryset = User.objects.filter(id=user.id) if user else None
        super(). __init__ (*args, **kwargs)
    

```

  

**Relevant Views**

```auto
from django.views import View
from websites.models import Website

class WebsiteCreateView(View):
    form_class = WebsiteCreateForm
    template_name = 'websites/create_view.html'
    
    def get(self, request):
        context = {'form': self.form_class(user=request.user)}
        return render(request, self.template_name, context)
    
    def post(self, request):
        form = self.form_class(request.POST)
        if form.is_valid():
            print(form.cleaned_data)
        context = {'form': self.form_class(user=request.user)}
        return render(request, self.template_name, context)

```

**When rendering the template, I get the following error**

```auto
AttributeError at /websites/create/
'WebsiteCreateForm' object has no attribute 'user'

```

  

**What am I trying to achieve here?**  
To pass the logged user to the form so It can be automatically assigned to the Website object that will be created.  
_Adding the logged user without displaying the user field in the form will be even better._

Does someone know how to accomplish this?  
If you have doubts about the code or the logic, feel free to ask.

---

<div class="post-metadata">

### Author: ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)
#### Post date: [January 26, 2023, 6:10pm UTC](https://forum.djangoproject.com/t/unable-to-assign-a-user-to-a-form-field-through-request-user/18459/2 "2023-01-26T18:10:58Z")

</div>

See the docs for [the save method](https://docs.djangoproject.com/en/4.1/topics/forms/modelforms/#the-save-method), particularly the second example block. (You can ignore the bit about the many-to-many field.)

Also, I’m guessing that WebsiteCreateForm is supposed to be a Model form? If not, then it’s still your responsibility to copy those field to the relevent models to be saved. A standard form doesn’t have anything to save data _to_.
