如何在Django 1.7中的表单模型中获取当前登录的用户ID? [英] How to get currently logged user id in form model in Django 1.7?
问题描述
假设我有一个显示歌曲的网页。假设有公开和私人歌曲。公开歌曲可供所有人查看,而私人歌曲是特定用户创建的歌曲,并且仅供他查看。因此,用户只能看到owner_id == NULL和owner_id == current_logged_in_user_id(自己的ID)的歌曲
型号:
import ....
class Song(models.Model):
name = models.CharField(max_length = 100)
持续时间= models.IntegerField(max_length = 15)
owner = models.ForeignKey(settings.AUTH_USER_MODEL,null = True,blank = True)
def __unicode __(self):
return self.name
视图:
django.shortcuts中的
导入渲染,重定向,get_object_or_404
django.contrib.auth.decorators导入login_required
从songapp.models导入Song
从songapp.forms导入SongInfoForm
@login_required
def song_info(request):
song = get_object_or_404(Box)
song_status = song.get_status()
form = SongInfoForm(initial = {'song_list':song.song_list})
return render(request,'songapp / song_info.html',
{'form':form,'song':song,'song_status':song_status})
表格:
从django导入表格
从django.forms导入ModelChoiceField
从songapp.models导入Song
类SongInfoForm(forms.Form):
- > selected_songs = Song.objects.filter(owner = None)| Song.objects.filter(owner = 3)
song_list = ModelChoiceField(queryset = selected_songs,required = False)
请注意Form文件中带有箭头的行。这就是问题所在。该代码现在可以使用,但是
(所有者= 3)
是硬编码的。我知道我的用户ID是3。但是我希望它能正常工作。应该是这样的:
(owner = current_logged_in_user.id)
我对Django和Python还是很陌生,我不知道如何将用户ID传递给SongInfoForm FormModel。
解决方案我已经解决了。
在views.py中进行更改:
form = SongInfoForm(initial = {'song_list':song.song_list},user = request.user)
感谢之前的回答和本示例
我已经想到了这一点,并且
在forms.py
class SongInfoForm(forms.Form):
def __init __(self,* args,** kwargs):
user = kwargs.pop('user')
super (SongInfoForm自身)。__init __(* args,** kwargs)
self.fields [’song_list’] = form.ModelChoiceField(queryset = Song.objects.filter(owner = None)| Song.playlist.objects.filter(owner = user),required = False)
Let's say I have a webpage that displays songs. And let's say there are public and private songs. Public songs are available for everyone to see, while private songs are songs that a certain user has created and are only available for him to see. So the user should only see those songs with the owner_id == NULL and owner_id == currently_logged_in_user_id (his own id)
Model:
import .... class Song(models.Model): name = models.CharField(max_length=100) duration = models.IntegerField(max_length=15) owner = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True) def __unicode__(self): return self.name
View:
from django.shortcuts import render, redirect, get_object_or_404 from django.contrib.auth.decorators import login_required from songapp.models import Song from songapp.forms import SongInfoForm @login_required def song_info(request): song = get_object_or_404(Box) song_status = song.get_status() form = SongInfoForm(initial={'song_list': song.song_list}) return render(request, 'songapp/song_info.html', {'form': form, 'song': song, 'song_status': song_status})
Form:
from django import forms from django.forms import ModelChoiceField from songapp.models import Song class SongInfoForm(forms.Form): --> selected_songs = Song.objects.filter(owner=None) | Song.objects.filter(owner=3) song_list = ModelChoiceField(queryset=selected_songs, required=False)
Note the line with the arrow in the Form file. This is where the problem lies. The code works now, but the
(owner = 3)
is hardcoded. I know for a fact that my users id is 3. But I want it to work properly. It should be something like this:
(owner = currently_logged_in_user.id)
I'm still very new to Django and Python and I don't know how to pass the users id to the SongInfoForm FormModel.
解决方案I've figured it out.
In views.py change:
form = SongInfoForm(initial={'song_list': song.song_list}, user=request.user)
And thanks to the answers before and this example django form: Passing parameter from view.py to forms gives out error I've came up with this, and it works like a charm.
In forms.py
class SongInfoForm(forms.Form): def __init__(self, *args, **kwargs): user = kwargs.pop('user') super(SongInfoForm, self).__init__(*args, **kwargs) self.fields['song_list'] = forms.ModelChoiceField(queryset=Song.objects.filter(owner=None) | Song.playlist.objects.filter(owner=user), required=False)
这篇关于如何在Django 1.7中的表单模型中获取当前登录的用户ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!