title: How to solve Value Error assign error in Django?
body: <p>I am creating a system with Django. I create a system for limiting. I have several company, and every company has to create their own limit criteria. I think I can use foreign key for that. What I mean is I want to take company name from user's company name. You can understand clearly from my models.
  But user should not see <code>comp_name</code> field in their page, it should only be seen from the admin page (kind of a hidden field in frontpage.</p>
  <p>I tried a model for what I want but I get an error:</p>
  <blockquote>
  <p>ValueError at /add/limit Cannot assign &quot;'Test 1'&quot;:
  &quot;DoaTable.comp_name&quot; must be a &quot;CompanyProfile&quot; instance.</p>
  </blockquote>
  <p>how can I solve it?</p>
  <p><strong>views. py</strong></p>
  <pre><code>def create_doa_table(request):
      form_class = DoaTableForm
      current_user = request.user
      userP = UserProfile.objects.get_or_create(username=current_user)
      companyName = userP[0].comp_name
      # If this is a POST request then process the Form data
      if request.method == 'POST':
          # Create a form instance and populate it with data from the request (binding):
          form = DoaTableForm(request.POST)
          # Check if the form is valid:
          if form.is_valid():
              newDoaElement = form.save()
              newDoaElement.comp_name = companyName
              newDoaElement.save()
  
              # redirect to a new URL:
              return render(request, 'doa_table.html')
      else:
          form = form_class()
  
      return render(request, 'add_doa_element.html', {'form': form})
  </code></pre>
  <p><strong>models.py</strong></p>
  <pre><code>class DoaTable(models.Model):
      rank = models.CharField(max_length=20)
      risk = models.CharField(max_length=20)
      limit = models.CharField(max_length=20)
      comp_name = models.ForeignKey(CompanyProfile, on_delete=models.CASCADE, null=True)
  
      def __str__(self):
          return self.rank
  </code></pre>
  <hr />
  <pre><code>class UserProfile(AbstractUser):
      
      comp_name = models.CharField(max_length=200, default='', blank=True, null=True)
      user_id = models.UUIDField(default=uuid.uuid4(), editable=False, unique=True)
      username = models.CharField(max_length=500, unique=True)
      ...
  
      def __str__(self):
          return self.username
  </code></pre>
  <p><strong>forms.py</strong></p>
  <pre><code>class DoaTableForm(forms.ModelForm):
      class Meta:
          model = DoaTable
          comp_name = forms.ModelChoiceField(queryset=CompanyProfile.objects.all(), required=False,
                                             widget=forms.HiddenInput())
          fields = ('rank', 'risk', 'limit', )
  </code></pre>
  <p><strong>traceback</strong></p>
  <pre><code>Environment:
  
  
  Request Method: POST
  Request URL: http://127.0.0.1:8000/add/limit
  
  Django Version: 3.1.4
  Python Version: 3.8.7
  Installed Applications:
  ['django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'register',
   'customer',
   'financial_analysis',
   'ocr',
   'core',
   'approvals',
   'django_tables2',
   'crispy_forms',
   'ckeditor',
   'rest_framework',
   'requests']
  Installed Middleware:
  ['django.middleware.security.SecurityMiddleware',
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.common.CommonMiddleware',
   'django.middleware.csrf.CsrfViewMiddleware',
   'django.contrib.auth.middleware.AuthenticationMiddleware',
   'django.contrib.messages.middleware.MessageMiddleware',
   'django.middleware.clickjacking.XFrameOptionsMiddleware']
  
  
  
  Traceback (most recent call last):
    File &quot;C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\core\handlers\exception.py&quot;, line 47, in inner
      response = get_response(request)
    File &quot;C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\core\handlers\base.py&quot;, line 179, in _get_response
      response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File &quot;C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\contrib\auth\decorators.py&quot;, line 21, in _wrapped_view
      return view_func(request, *args, **kwargs)
    File &quot;C:\Users\USER\OneDrive\Documents\GitHub\otc\approvals\views.py&quot;, line 29, in create_doa_table
      newDoaElement.comp_name = companyName
    File &quot;C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\db\models\fields\related_descriptors.py&quot;, line 215, in __set__
      raise ValueError(
  
  Exception Type: ValueError at /add/limit
  Exception Value: Cannot assign &quot;'Test 1'&quot;: &quot;DoaTable.comp_name&quot; must be a &quot;CompanyProfile&quot; instance.
  </code></pre>