Apr-02-2019, 11:34 AM
How can I get data from a form (ProductCreateForm)?
If I write form = self.get_form(), then I just get a form template, where some data is selected, and some are not (select especially).
If I write form = ProductCreateForm(request.POST), then I get an error saying that the request was not found. Perhaps this is due to the fact that I set the request in get_context_data() and work with them in the __init__ method in the forms.py.
I process the data in the clean method in the forms.py.
If I write form = self.get_form(), then I just get a form template, where some data is selected, and some are not (select especially).
If I write form = ProductCreateForm(request.POST), then I get an error saying that the request was not found. Perhaps this is due to the fact that I set the request in get_context_data() and work with them in the __init__ method in the forms.py.
I process the data in the clean method in the forms.py.
class ProductsCreate(CreateView): model = Product form_class = ProductCreateForm http_method_names = ['get', 'post'] def get_context_data(self, *args, **kwargs): ctx=super(ProductsCreate, self).get_context_data(*args, **kwargs) ctx['special_form'] = SpeciallyPriceForm() return ctx def get(self, request, *args, **kwargs): self.object = None if kwargs.get('slug'): category = Category.objects.filter(slug=kwargs.get('slug')).first() self.initial.update({'category': category}) return self.render_to_response(self.get_context_data()) def post(self, request, *args, **kwargs): self.object = None form = self.get_form() #Тут что-то не то, видимо... special_form = SpeciallyPriceForm(self.request.POST) if form.is_valid() and special_form.is_valid(): return self.form_valid(form) else: return self.form_invalid(form)
class ProductCreateForm(forms.ModelForm): #.... def __init__(self, *args, **kwargs): self.request = kwargs.pop('request') #... user = self.request.user provider = Provider.objects.filter(user=user.id).last() self.fields['category'] = ModelMultipleChoiceField(queryset=provider.category.all()) #... def clean(self): cleaned_data = super(ProductCreateForm, self).clean() cd_category = cleaned_data.get('category') #... class SpeciallyPriceForm(forms.ModelForm): class Meta: model = SpeciallyPrice fields = ['adittional_specially_price', 'adittional_specially_number']