如何在Django CreateView,UpdateView中过滤值

时间:2022-09-01 07:24:58

I am trying to use CreateView and UpdateView to modify records. My problem is that the ForeignKey dropdown lists values (Psa.number) for all companies instead only those of the company to which the user belongs.

我正在尝试使用CreateView和UpdateView来修改记录。我的问题是ForeignKey下拉列表列出了所有公司的值(Psa.number),而只列出了用户所属公司的值。

class Psa(models.Model):
    owner = models.ForeignKey(Employer)
    number = models.PositiveIntegerField(unique=True...
    type = models.CharField(max_length=6 ...

class Employer(models.Model):
employer_name = models.CharField(max_length=100, unique=True)

The form:

class PsaCreateForm(forms.ModelForm):
class Meta:
    model = Psa
    fields = [
        'number',
        'type',
    ]

What is the best way to solve this? I've got several other conditions that use the same company foreignkey relationship so is there a way to create a method on the model that I can reuse?

解决这个问题的最佳方法是什么?我有几个使用相同公司外键关系的其他条件,那么有没有办法在模型上创建一个我可以重用的方法?

1 个解决方案

#1


0  

You need get_form_kwargs(self) in CreateView and UpdateView:

在CreateView和UpdateView中需要get_form_kwargs(self):

forms.py:

class PsaCreateForm(forms.ModelForm):

    model = Psa
    fields = [
        'number',
        'type',
    ]

    def __init__(self, *args, **kwargs):
        self.current_user = kwargs.pop('user')
        super(PsaCreateForm, self).__init__(*args, **kwargs)
        if self.current_user:
            self.fields['number'].queryset = Psa.objects.filter(owner=self.current_user)

views.py

class PsaCreate(CreateView):
    model = Psa
    template_name = 'form.html'
    form_class = PsaCreateFrom

    def get_form_kwargs(self):
        kwargs = super(PsaCreate, self).get_form_kwargs()
        kwargs['user'] = self.request.user
        return kwargs

Also a good idea. Set def dispatch(self, request, *args, **kwargs): in your Views.

也是个好主意。在你的视图中设置def dispatch(self,request,* args,** kwargs):

def dispatch(self, request, *args, **kwargs):
    obj = self.get_object()
    if obj.owner != self.request.user:
        return HttpResponseRedirect(reverse('home'))
    return super(PsaUpdate, self).dispatch(request, *args, **kwargs)

#1


0  

You need get_form_kwargs(self) in CreateView and UpdateView:

在CreateView和UpdateView中需要get_form_kwargs(self):

forms.py:

class PsaCreateForm(forms.ModelForm):

    model = Psa
    fields = [
        'number',
        'type',
    ]

    def __init__(self, *args, **kwargs):
        self.current_user = kwargs.pop('user')
        super(PsaCreateForm, self).__init__(*args, **kwargs)
        if self.current_user:
            self.fields['number'].queryset = Psa.objects.filter(owner=self.current_user)

views.py

class PsaCreate(CreateView):
    model = Psa
    template_name = 'form.html'
    form_class = PsaCreateFrom

    def get_form_kwargs(self):
        kwargs = super(PsaCreate, self).get_form_kwargs()
        kwargs['user'] = self.request.user
        return kwargs

Also a good idea. Set def dispatch(self, request, *args, **kwargs): in your Views.

也是个好主意。在你的视图中设置def dispatch(self,request,* args,** kwargs):

def dispatch(self, request, *args, **kwargs):
    obj = self.get_object()
    if obj.owner != self.request.user:
        return HttpResponseRedirect(reverse('home'))
    return super(PsaUpdate, self).dispatch(request, *args, **kwargs)