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:
Antje
On 4/28/20 6:56 PM, Sergei Sokov wrote:
--
forms.py
class OrderForm(forms.ModelForm):
class Meta:
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
class OrderNewBigPrintView(LoginRequiredMixin, CustomSuccessMessageMixin, CreateView):
model = Order
template_name = 'new_order_bp.html'
form_class = OrderForm
success_url = reverse_lazy('orders')
success_msg = 'Ok'
models.py
class TypJob(models.Model):
author = models.ForeignKey(User, on_delete = models.CASCADE, verbose_name='author', null=True)
name_job = models.CharField('name job', max_length=200)
class Order(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 = Orderfields = '__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...@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
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/bbbdf319-812c-40e0-9ebf-235efed46a9d%40googlegroups.com.
No comments:
Post a Comment