Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
Manually enter a value in django model form

I am using django's default User class. I have made a UserImage class to store users' images.

class UserImage(models.Model):
    user_id = models.ForeignKey(User, blank=False)
    image_url = models.ImageField(upload_to='users/images/')

In the ImageUploadForm form, I only want user to chose the image_url, and set user_id to currently authenticated user's id. This is the image upload form,

class ImageUploadForm(forms.ModelForm):
    class Meta:
        model = UserImage
        fields = {'image_url'}

Normally I save model forms like this,

form = ProjectForm(request.POST or None, instance=project)

    if request.POST and form.is_valid():
        form.save()
        return HttpResponseRedirect(reverse('projects:project_dashboard', kwargs={'project_id': project_id}))

So how do I include the user_id field here? Should I make this a hidden field in the view? Or is there a better choice?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I believe the image will be uploaded by authenticated user,right? I mean only after login, he/she can upload image. In that case you can simply use request.user in your user parameter.

if form.is_valid():
   user_image = form.save(commit=False)
   user_image.user_id = request.user.id
   user_image.save()
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!