Tuesday, April 28, 2020

Re: How to use ModelChoiceField? I whould like to filter the output from a field form.

the form doesn't know the request only the view does. how does your view function look like?

On 4/28/20 5:57 PM, Sergei Sokov wrote:
я не совсем понял

    class Meta:
        model = Order
        fields = '__all__'
    name_job = forms.ModelMultipleChoiceField(queryset=None)
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        qs = TypJob.objects.filter(author__id=request.user.id)
        self.fields['name_job'].queryset = qs

I have error
'NoneType' object has no attribute '_prefetch_related_lookups'


вторник, 28 апреля 2020 г., 17:19:01 UTC+2 пользователь hend hend написал:
user.id надо брать из request объекта в контроллере request.user.id. В том же контроллере метод get_initial() позволяет инициализировать поля формы(указанной в аттрибуте "form_class")контроллера.
Например:
    def get_initial(self):
            # поле category формы будет установлено в результат выборки по ключу
            self.initial["category"] = Category.objects.get(pk=self.c_id)
        return self.initial.copy() # 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/12538865-4221-40e9-92bf-b8efc1ed7309%40googlegroups.com.

No comments:

Post a Comment