no idea how you do this with a CreateView but with a simple view function you have a request object:
https://docs.djangoproject.com/en/3.0/topics/http/views/#a-simple-view this thread is about passing a variable into from the view function to a form: https://groups.google.com/forum/#!searchin/django-users/I$20think$20in$20your$20view$20modulesView()$20%7Csort:date/django-users/AtTDAn-M7_E/V7sx7piSCQAJ Antje On 4/28/20 6:56 PM, Sergei Sokov wrote: > | > forms.py > classOrderForm(forms.ModelForm): > classMeta: > model =Order > name_job =forms.ModelMultipleChoiceField(queryset=None) > def__init__(self,*args,**kwargs): > super().__init__(*args,**kwargs) > qs =TypJob.objects.filter(author__id=request.user.id) > | > > views.py > | > classOrderNewBigPrintView(LoginRequiredMixin,CustomSuccessMessageMixin,CreateView): > model =Order > template_name ='new_order_bp.html' > form_class =OrderForm > success_url =reverse_lazy('orders') > success_msg ='Ok' > | > > | > models.py > classTypJob(models.Model): > author =models.ForeignKey(User,on_delete > =models.CASCADE,verbose_name='author',null=True) > name_job =models.CharField('name job',max_length=200) > > > classOrder(models.Model): > author =models.ForeignKey(User,on_delete > =models.CASCADE,verbose_name='author',blank=True,null=True) > number_order =models.CharField('number of order',max_length=100) > date_create =models.DateTimeField(auto_now=True) > name =models.ForeignKey(Customer,on_delete > =models.CASCADE,verbose_name='customer',null=True) > name_order =models.CharField('name of order',max_length=200) > | > > > вторник, 28 апреля 2020 г., 18:41:25 UTC+2 пользователь Antje > Kazimiers написал: > > 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 >> <http://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 <http://user.id> надо брать из request объекта в >> контроллере request.user.id <http://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 [email protected]. >> To view this discussion on the web visit >> >> https://groups.google.com/d/msgid/django-users/12538865-4221-40e9-92bf-b8efc1ed7309%40googlegroups.com >> >> <https://groups.google.com/d/msgid/django-users/12538865-4221-40e9-92bf-b8efc1ed7309%40googlegroups.com?utm_medium=email&utm_source=footer>. > > -- > 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 [email protected] > <mailto:[email protected]>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/bbbdf319-812c-40e0-9ebf-235efed46a9d%40googlegroups.com > <https://groups.google.com/d/msgid/django-users/bbbdf319-812c-40e0-9ebf-235efed46a9d%40googlegroups.com?utm_medium=email&utm_source=footer>. -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/595f75bc-0dc0-0794-1852-e8745d8e4dee%40gmail.com.

