在Django ModelForm中使用request.user [英] Using request.user with Django ModelForm

查看:203
本文介绍了在Django ModelForm中使用request.user的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个登录用户和Django ModelForm 的问题。我有一个名为 Animal 的类,其中有一个 ForeignKey 用户和一些与动物像年龄,种族等等。用户可以将动物添加到数据库,我必须跟踪每个动物的作者,所以我需要添加用户创建动物实例时记录的 request.user

  class Animal(models.Model):
name = models.CharField(max_length = 300)
age = models。 PositiveSmallIntegerField()
race = models.ForeignKey(Race)
...
publisher = models.ForeignKey(User)
def __unicode __(self):
return self .name

class AnimalForm(ModelForm):
class Meta:
model = Animal

主要目标是隐藏表单中的发布商字段,并在点击保存按钮时提交记录的用户。



我可以抓住当前用户在视图中使用初始,但我还想要的不显示该字段。

  @login_required 
def new_animal(request):
如果request.method ==POST:
form = AnimalForm (request.POST)
如果form.is_valid():
form.save()
返回重定向('/')
else:
variables = RequestContext请求,{'form':form})
return render_to_response('web / animal_form.html',variables)
else:
form = AnimalForm(initial = {'publisher'用户})
variables = RequestContext(request,{'form':form})
返回render_to_response('web / animal_form.html',变量)
/ pre>

解决方案

您只需将其从表单中排除,然后将其设置在视图中。

  class AnimalForm(ModelForm):
class Meta:
model = Animal
exclude =('publisher',)

...在视图中:

  form = AnimalForm(request.POST)
如果form.is_valid():
animal = form.save(commit = false)
animal.publisher = request.user
animal.save()

(另请注意第一个 else 子句 - 紧跟重定向后的行是不必要的。如果你把它留下来,执行将落到视图末尾的两行,这是相同的。)


I'm having a problem with logged users and a Django ModelForm. I have a class named Animal that has a ForeignKey to User and some data related to the animal like age, race, and so on. A user can add Animals to the DB and I have to track the author of each animal, so I need to add the request.user that is logged when the user creates an animal instance.

class Animal(models.Model):
    name = models.CharField(max_length=300)
    age = models.PositiveSmallIntegerField()
    race = models.ForeignKey(Race)
    ...
    publisher = models.ForeignKey(User)
    def __unicode__(self):
        return self.name

class AnimalForm(ModelForm):
    class Meta:
        model = Animal

The main goal is hide the publisher field in the form, and submit the logged user when hitting save button.

I can catch the current user in the view using initial, but what I also want is not display the field.

@login_required
def new_animal(request):
    if request.method == "POST":
        form = AnimalForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/')
        else:
            variables = RequestContext(request, {'form': form})
            return render_to_response('web/animal_form.html', variables)
    else:
        form = AnimalForm(initial={'publisher': request.user})
    variables = RequestContext(request, {'form': form})
    return render_to_response('web/animal_form.html', variables)

解决方案

You just need to exclude it from the form, then set it in the view.

class AnimalForm(ModelForm):
    class Meta:
        model = Animal
        exclude = ('publisher',)

... and in the view:

    form = AnimalForm(request.POST)
    if form.is_valid():
        animal = form.save(commit=false)
        animal.publisher = request.user
        animal.save()

(Note also that the first else clause - the lines immediately following the redirect - is unnecessary. If you leave it out, execution will fall through to the two lines at the end of the view, which are identical.)

这篇关于在Django ModelForm中使用request.user的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆