在Django中,如何将ForeignKey传递给Model的实例?

时间:2022-03-28 02:17:27

I am writing an application which stores "Jobs". They are defined as having a ForeignKey linked to a "User". I don't understand how to pass the ForeignKey into the model when creating it. My Model for Job worked fine without a ForeignKey, but now that I am trying to add users to the system I can't get the form to validate.

我正在写一个存储“Jobs”的应用程序。它们被定义为具有链接到“用户”的ForeignKey。我不明白在创建它时如何将ForeignKey传递给模型。我的Model for Job在没有ForeignKey的情况下运行良好,但现在我正在尝试将用户添加到系统中,我无法获得要验证的表单。

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 was creating the objects as follows:

在views.py中,我创建了如下对象:

if request.method == 'POST':
    form = JobForm(request.POST, request.FILES)
    if form.is_valid():
        #Do something here

I understand that this passes the form data and the uploaded files to the form. However, I don't understand how to pass in a User to be set as the ForeignKey.

据我所知,这会将表单数据和上传的文件传递给表单。但是,我不明白如何传入用户设置为ForeignKey。

Thanks in advance to anyone who can help.

提前感谢任何可以提供帮助的人。

2 个解决方案

#1


7  

A typical pattern in Django is:

Django中的典型模式是:

  1. exclude the user field from the model form
  2. 从模型表单中排除用户字段

  3. save the form with commit=False
  4. 使用commit = False保存表单

  5. set job.user
  6. save to database
  7. 保存到数据库

In your case:

在你的情况下:

class JobForm(forms.ModelForm):
    class Meta:
        model = Job
        exclude = ('user',)

if request.method == 'POST':
    form = JobForm(request.POST, request.FILES)
    job = form.save(commit=False)
    job.user = request.user
    job.save()
    # the next line isn't necessary here, because we don't have any m2m fields
    form.save_m2m()

See the Django docs on the model form save() method for more information.

有关更多信息,请参阅模型表单save()方法中的Django文档。

#2


1  

Try:

if request.method == 'POST':
    data = request.POST
    data['user'] = request.user
    form = JobForm(data, request.FILES)
    if form.is_valid():
        #Do something here

#1


7  

A typical pattern in Django is:

Django中的典型模式是:

  1. exclude the user field from the model form
  2. 从模型表单中排除用户字段

  3. save the form with commit=False
  4. 使用commit = False保存表单

  5. set job.user
  6. save to database
  7. 保存到数据库

In your case:

在你的情况下:

class JobForm(forms.ModelForm):
    class Meta:
        model = Job
        exclude = ('user',)

if request.method == 'POST':
    form = JobForm(request.POST, request.FILES)
    job = form.save(commit=False)
    job.user = request.user
    job.save()
    # the next line isn't necessary here, because we don't have any m2m fields
    form.save_m2m()

See the Django docs on the model form save() method for more information.

有关更多信息,请参阅模型表单save()方法中的Django文档。

#2


1  

Try:

if request.method == 'POST':
    data = request.POST
    data['user'] = request.user
    form = JobForm(data, request.FILES)
    if form.is_valid():
        #Do something here