I
I
Ilyas2017-10-02 16:27:38
PHP
Ilyas, 2017-10-02 16:27:38

Is it possible to replace a model form field in Django?

there is a form, the 'sizes' field is present in the model as ManyToManyField

class AddForm(forms.ModelForm):
    quantity = forms.IntegerField(min_value=1, initial=1)

    class Meta:
        model = Product
        fields = (
            'sizes',
            'quantity',
        
        def __init__(self, *args, **kwargs):
            product_id = kwargs.pop('product_id', None)
            super(AddForm, self).__init__(*args, **kwargs)
            product = get_object_or_404(Product, id=product_id)
            self.fields['sizes'] = forms.ModelChoiceField(queryset=product.sizes.all())

how can I hide the sizes field when rendering the page and set a specific value for it,
I did this:
self.fields['sizes'] = forms.BooleanField(initial=False, widget=forms.HiddenInput)

but in this case a hidden input appears, where value="", i.e. an empty request, although I specify that initial=False
is needed in order not to show the "size" field for products where it is not needed.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lazy @BojackHorseman PHP, 2019-06-23
@Tolly

specify the local path to the file in fopen, not url

T
Talyan, 2019-06-23
@flapflapjack

you need to send the right header:
https://stackoverflow.com/questions/8485886/force-...

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file_url)); //Absolute URL
ob_clean();
flush();
readfile($file_url); //Absolute URL
exit();

just send these headlines with a curl.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question