0 I'm trying to build a form that automatically fills some fields in a class based create view that lets logged in users create a Job. However, I can't seem to find the correct way of doing this for fields that aren't the user (eg. request.user).
So the create view is trying to get a company_name from the logged in user, company_name field belongs to a model called Company. Each Company has an owner with a foreign key to the User model. All the reuest I've tried so far have led to a 'WSGIRequest' error. So far I've tried to request: company_name user.company_name company.company_name company user I don't understand how these requests work, I have seen examples for getting the logged in users name and I'm not familiar how to do this otherwise. Please can someone help me understand how this works and how I should be doing this? I'm using Django 2.2 with python 3.6 Auth Models: class User(AbstractBaseUser, PermissionsMixin): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) phone_number = models.CharField(max_length=15) email = models.EmailField(max_length=250, unique=True) is_staff = models.BooleanField(default=False) is_superuser = models.BooleanField(default=False) is_active = models.BooleanField(default=True) last_login = models.DateTimeField(null=True, blank=True) date_joined = models.DateTimeField(auto_now_add=True) objects = UserManager() USERNAME_FIELD = 'email' EMAIL_FIELD = 'email' def __str__(self): return self.first_name def get_absolute_url(self): return "/users/%i/" % self.pk class Company(models.Model): owner = models.ForeignKey(User, on_delete=models.CASCADE) company_name = models.CharField(max_length=100, unique=True) company_address = models.CharField(max_length=100, unique=True) company_website = models.URLField(blank=True, null=True) company_verified = models.BooleanField(default=False) def __str__(self): return self.company_name View: class JobCreateView(LoginRequiredMixin, CreateView): model = Job form_class = JobForm template_name = 'jobs/job_create.html' def form_valid(self, form): form.instance.company_name = self.request.user.company form.instance.job_reference = self.request.job.pk return super(JobCreateView, self).form_valid(form) def get_success_url(self): return reverse('jobs:job_details', kwargs={'pk': self.object.pk}) Other Model: class Job(models.Model): JOB_TYPE = ( ('1', 'Service'), ('2', 'Repair'), ('3', 'Quotation'), ('4', 'Consultation'), ('5', 'Report'), ('6', 'Design'),) ACCOUNT_TYPE = ( ('1', 'Existing Customer'), ('2', 'Charge to Account'), ('3', 'New Customer'), ('4', 'Pre-Paid/C.O.D'), ('5', 'Issued and Acc App'),) company_name = models.ForeignKey(Company, related_name='jobs', verbose_name="Company Name", on_delete=models.CASCADE) job_reference = models.CharField(max_length=50, blank=False) contact_person = models.CharField(max_length=50) contact_number = models.IntegerField() contact_person_email = models.EmailField(max_length=100, blank=True, null=True) site_address = models.CharField(max_length=100) job_type = models.CharField(choices=JOB_TYPE, max_length=50, default='1') account_type = models.CharField(choices=ACCOUNT_TYPE, max_length=50, default='1') job_details = models.CharField(max_length=1000) created = models.DateTimeField(default=now, blank=True) def __str__(self): return str(self.company_name) def get_absolute_url(self): return reverse('jobs:detail', kwargs={'pk': self.pk}) -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a0d6959d-380f-4bb6-8d46-a6a385142975%40googlegroups.com.