I am writing an application in which "Works" are stored. They are defined as having ForeignKeys associated with the "User". I do not understand how to pass ForeignKey to the model when creating it. My model for work worked fine without ForeignKey, but now when I try to add users to the system, I canβt get the form for verification.
models.py:
from django.db import models from django import forms from django.contrib.auth.models import User class Job(models.Model): user = models.ForeignKey(User) name = models.CharField(max_length=50, blank=True) pub_date = models.DateTimeField('date published', auto_now_add=True) orig_image = models.ImageField('uploaded image', upload_to='origImageDB/', blank=True) clean_image = models.ImageField('clean image', upload_to='cleanImageDB/', blank=True) fullsize_image = models.ImageField('fullsize image', upload_to='fullsizeImageDB/') fullsize_clean_image = models.ImageField('fullsize clean image', upload_to='fullsizeCleanImageDB/') regions = models.TextField(blank=True) orig_regions = models.TextField(blank=True) class JobForm(forms.ModelForm): class Meta: model = Job
In views.py, I created the objects as follows:
if request.method == 'POST': form = JobForm(request.POST, request.FILES) if form.is_valid():
I understand that this passes form data and uploaded files to the form. However, I do not understand how to transfer the user to be installed as ForeignKey.
Thanks in advance to everyone who can help.
source share