限制ManyToMany ForeignKey显示的选项

时间:2015-12-09 01:58:54

标签: django django-forms

如何限制通过ManyToMany关系返回的值,从而显示在表单的 dtbind.Columns.Add("Id"); dtbind.Columns["Id"].DisplayIndex = 0; dtbind.Columns.Add("Qnt"); dtbind.Columns["Qnt"].DisplayIndex = 1; //Like onwards 字段中,仅显示当前登录用户创建的点?

models.py

<SELECT>

forms.py

class Project(models.Model):
    owner = models.ForeignKey(User, editable=False)
    ...
    spots = models.ManyToManyField(to='Spot', blank=True, )

    class Spot(models.Model):
        owner = models.ForeignKey(User, editable=False)
        spot_name = models.CharField(max_length=80, blank=False)

我正在使用GenericViews进行更新和创建,目前在我更新或创建项目时会看到每个人都在Spots中创建的所有条目。我只想查看登录用户输入的条目。为了完整起见,是的,from django import forms from .models import Project, Spot class ProjectForm(forms.ModelForm): class Meta: model = Project exclude = ('owner', ) class SpotForm(forms.ModelForm): class Meta: model = Spot exclude = ('owner', ) project.owner在创建时设置为用户。

我在forms.py中尝试了def INIT ,并在模型中的manytomany字段中使用了limit_choices_to。要么我做错了,要么就是不正确的做法。

谢谢你!

2 个答案:

答案 0 :(得分:1)

在您的forms.py

class ProjectForm(forms.ModelForm):
    class Meta:
        model = Project
        exclude = ('owner', )

    def __init__(self, user_id, *args, **kwargs):
        self.fields['spots'] = forms.ModelChoiceField(widget=forms.Select, queryset=Project.objects.filter(owner=user_id))


class SpotForm(forms.ModelForm):
    class Meta:
        model = Spot
        exclude = ('owner', )

    def __init__(self, user_id, *args, **kwargs):
        self.fields['spot_name'] = forms.ModelChoiceField(widget=forms.Select, queryset=Spot.objects.filter(owner=user_id))
你的views.py中的

user_id = Project.objects.get(owner=request.user).owner
project_form = ProjectForm(user_id)
spot_form = SpotForm(user_id)

答案 1 :(得分:1)

正如我上面提到的,Dean的答案非常接近,但对我来说并不适用。主要是因为请求无法直接在视图中访问。也许是在较旧的Django版本中?我在1.9。谢谢Dean,你让我超越了驼峰!

正在发生的事情是将User添加到View中的kwargs中,将其传递给ModelForm,从kwargs中删除User并在显示表单之前使用它来过滤Spots。

这是适用于我的项目的代码:

views.py

class ProjectUpdate(UpdateView):
    model = Project
    success_url = reverse_lazy('projects-mine')
    form_class = ProjectForm

def dispatch(self, *args, **kwargs):
    return super(ProjectUpdate, self).dispatch(*args, **kwargs)

def get_form_kwargs(self):
    kwargs = super(ProjectUpdate, self).get_form_kwargs()
    kwargs.update({'user': self.request.user})
    return kwargs

forms.py

class ProjectForm(forms.ModelForm):
    class Meta:
        model = Project
        exclude = ('owner', 'whispir_id')

    def __init__(self, *args, **kwargs):
        user_id = kwargs.pop('user')
        super(ProjectForm, self).__init__(*args, **kwargs)
        self.fields['spots'] = forms.ModelMultipleChoiceField(queryset=Spot.objects.filter(owner=user_id))


class SpotForm(forms.ModelForm):
    class Meta:
        model = Spot
        exclude = ('owner', )

    def __init__(self, *args, **kwargs):
        user_id = kwargs.pop('user')
        super(SpotForm, self).__init__(*args, **kwargs)
        self.fields['spot_name'] = forms.ModelMultipleChoiceField(queryset=Spot.objects.filter(owner=user_id))