Tuesday, April 28, 2020

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

qs = TypJob.objects.filter(author__id=user.id)

needs to go inside of the __init__ function. you pass user = request.user as a parameter to OrderTestForm when you instantiate it in your view and can use it then in your init function.
Also you can only filter the id of the author if the author field is a ForeignKey, is it?

Antje

On Tue, Apr 28, 2020 at 9:35 AM Sergei Sokov <sokov.tlt@gmail.com> wrote:

I have some models classes. And some classes have bounding fields with another class. Each class have the field - author. I whould like that for each user the form bounding field shows only those data which was created these authors.

For example:
class "TypeJob" has field "author".
User Jhon created a TypJob - "Painting".
User Alex created a TypJob - "Cleaning".
Class "Order" has bounding field - "name_typ_job"
When Jhon whould like create an order, he opens the form and click "name type job" field, he sees both type job: painting and cleaning.
I whould like that he sees and could choose only "painting". Because he is it author.


I wrote that

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

But I have

name 'user' is not defined

name 'author__id' is not defined

--
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/178dcdb1-c119-46ef-83c3-95bda19118c8%40googlegroups.com.

--
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/CAN6tUhmTd%3DmSr5VvbrBhXMWaOBN5eyzhwSaH_ZkW2ZcEYTiaPA%40mail.gmail.com.

No comments:

Post a Comment