Django 1.3 CreateView、ModelForm 和通过 request.user 过滤字段

发布于 2024-11-26 23:41:01 字数 1692 浏览 0 评论 0原文

我正在尝试过滤 ModelForm 上的字段。我正在为我的视图创建通用 CreateView 的子类。我在网上找到了许多关于我的问题的参考资料,但这些解决方案似乎不适用于 Django 1.3 的基于类的视图(至少对我来说)。

这是我的模型:

#models.py

class Subscriber(models.Model):

    user = models.ForeignKey(User)
    subscriber_list = models.ManyToManyField('SubscriberList')
    ....

class SubscriberList(models.Model):

    user = models.ForeignKey(User)
    name = models.CharField(max_length=70)
....

这是我的观点:

#views.py

class SubscriberCreateView(AuthCreateView):
    model = Subscriber
    template_name = "forms/app.html"
    form_class = SubscriberForm
    success_url = "/app/subscribers/"

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.user = self.request.user
        return super(SubscriberCreateView, self).form_valid(form) 

这是我添加订阅者的原始表单,没有过滤器:

#forms.py

class SubscriberForm(ModelForm):

    class Meta:
        model = Subscriber
        exclude = ('user', 'facebook_id', 'twitter_id')

这是我修改后的表单,尝试过滤,但不起作用:

#forms.py

class SubscriberForm(ModelForm):

    class Meta:
        model = Subscriber
        exclude = ('user', 'facebook_id', 'twitter_id')

    def __init__(self, user, **kwargs):
        super(SubscriberForm, self).__init__(**kwargs)
        self.fields['subscriber_list'].queryset = SubscriberList.objects.filter(user=user)

如果我按如下方式更改此修改后的表单:

< code>def __init__(self, user=None, **kwargs)

它有效 - 它没有给我带来订阅者列表。但是,无论我尝试通过请求用户传递什么方式,我总是会收到名称“request”或名称“self”未定义的错误。

那么,我如何修改我的代码以通过request.user过滤subscriber_list,并且仍然使用Django 1.3的CreateView。

I am trying to filter a field on a ModelForm. I am subclassing the generic CreateView for my view. I found many references to my problem on the web, but the solutions do not seem to work (for me at least) with Django 1.3's class-based views.

Here are my models:

#models.py

class Subscriber(models.Model):

    user = models.ForeignKey(User)
    subscriber_list = models.ManyToManyField('SubscriberList')
    ....

class SubscriberList(models.Model):

    user = models.ForeignKey(User)
    name = models.CharField(max_length=70)
....

Here is my view:

#views.py

class SubscriberCreateView(AuthCreateView):
    model = Subscriber
    template_name = "forms/app.html"
    form_class = SubscriberForm
    success_url = "/app/subscribers/"

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.user = self.request.user
        return super(SubscriberCreateView, self).form_valid(form) 

Here is my original form for adding a Subscriber, with no filter:

#forms.py

class SubscriberForm(ModelForm):

    class Meta:
        model = Subscriber
        exclude = ('user', 'facebook_id', 'twitter_id')

Here is my modified form, attempting to filter, but doesn't work:

#forms.py

class SubscriberForm(ModelForm):

    class Meta:
        model = Subscriber
        exclude = ('user', 'facebook_id', 'twitter_id')

    def __init__(self, user, **kwargs):
        super(SubscriberForm, self).__init__(**kwargs)
        self.fields['subscriber_list'].queryset = SubscriberList.objects.filter(user=user)

If I change this modified form as so:

def __init__(self, user=None, **kwargs)

It works - It brings me NO subscriber lists. But any way I try to pass the request user, I invariably get a a name "request" or name "self" not defined error.

So, how can I modify my code to filter subscriber_list by the request.user, and still use Django 1.3's CreateView.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青瓷清茶倾城歌 2024-12-03 23:41:01

我看到你在不同的地方发布了这个问题..我发现这个问题的方式是因为我试图找出同样的事情。我认为我刚刚让它工作了,这就是我所做的。我重写了 FormMixin 中的 get_form() 来过滤特定的表单字段查询集:

class MyCreateView(CreateView):

    def get_form(self, form_class):
        form = super(MyCreateView,self).get_form(form_class) #instantiate using parent
        form.fields['my_list'].queryset = MyObject.objects.filter(user=self.request.user)
        return form

I see you've been posting this question in various places.. and the way I found that is because I was trying to figure out the same thing. I think I just got it working, and here's what I did. I overwrote get_form() from FormMixin to filter a specific form fields queryset:

class MyCreateView(CreateView):

    def get_form(self, form_class):
        form = super(MyCreateView,self).get_form(form_class) #instantiate using parent
        form.fields['my_list'].queryset = MyObject.objects.filter(user=self.request.user)
        return form
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文