[英]How to get currently logged user id in form model in Django 1.7?
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) 因此,用户只能看到owner_id == NULL和owner_id == current_logged_in_user_id(自己的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) (所有者= 3)
is hardcoded. 被硬编码。 I know for a fact that my users id is 3. But I want it to work properly. 我知道我的用户ID是3。但是我希望它能正常工作。 It should be something like this: 应该是这样的:
(owner = currently_logged_in_user.id) (所有者= current_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. 我对Django和Python还是很陌生,我不知道如何将用户ID传递给SongInfoForm FormModel。
I've figured it out. 我知道了。
In views.py change: 在views.py中进行更改:
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. 并感谢之前的回答和本示例django表单:将参数从view.py传递到表单会产生错误,我想出了这一点,它的工作原理很吸引人 。
In forms.py 在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)
OK, my bad. 好,我不好 Didn't read enought to see problem lies in the form, But according to this post: https://stackoverflow.com/a/5122029/2695295 you could rewrite your for like this: 没有足够的阅读以查看问题所在,但是根据这篇文章: https : //stackoverflow.com/a/5122029/2695295您可以这样重写您的代码:
class SongInfoForm(forms.Form):
def __init__(self, user, *args, **kwargs):
self.user = user
super(SongInfoForm, self).__init__(*args, **kwargs)
selected_songs = Song.objects.filter(owner=None) | Song.objects.filter(owner=user.id)
song_list = ModelChoiceField(queryset=selected_songs, required=False)
and then in view create your form like this: 然后在视图中创建您的表单,如下所示:
form = SongInfoForm(request.user, initial={'song_list': song.song_list})
this way form object should have access to user. 这样,表单对象应该可以访问用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.