bradleyg / django-s3direct

Directly upload files to S3 compatible services with Django.
MIT License
661 stars 234 forks source link

Modelform field override issue #142

Closed ethandrower closed 5 years ago

ethandrower commented 6 years ago

Hey everyone, I'm having a really tough time getting the s3 uploader widget to work within a modelform.

With a custom form, the widget is working, so I know my configuration is setup properly.

Here's the form I'm having trouble with.

class ManufacturerIntakeFormBase(ModelForm):
    class Meta:
        model = ManufacturerIntake
        fields = '__all__'
        widgets = {
        'technicalSheet': S3DirectWidget(dest='citemed_destination')
         }

class ManufacturerIntakeForm(ManufacturerIntakeFormBase):

     #techSheet = forms.URLField(widget=S3DirectWidget(dest='cite_destination'), label="Technical Sheet1", required=False)       
       label="Technical Sheet1", required=False) 

     class Meta(ManufacturerIntakeFormBase.Meta):

          fields = ManufacturerIntakeFormBase.Meta.fields 
         #fields = ManufacturerIntakeFormBase.Meta.fields + ['techSheet']

I've also tried the config without the widget specification in ManufacturerIntakeFormBase, and removing the two commented lines.

Any thoughts or input would be greatly appreciated. Thank you!

solrates commented 6 years ago

Hi Ethan,

Try defining the destination in the model, then use whatever type of file upload widget you like in the form.


# models.py
# Normal imports
from s3direct.fields import S3DirectField

class ManufacturerIntake(models.Model)
    technicalSheet = S3DirectField(verbose_name="Technical Sheet", dest='citemed_destination') # You may want to change this field name to technical_sheet for Django's naming conventions)

# forms.py
from .models import ManufacturerIntake

class ManufacturerIntakeFormBase(ModelForm):
    class Meta:
        model = ManufacturerIntake
        fields = '__all__'
        widgets = {
        'technicalSheet': ClearableFileInput() # or whatever you file upload widget you prefer
         }

Good luck!

Cheers,

Dustin

Timtech4u commented 6 years ago

Hello, I've the same, upload is successful when using the Django Admin.

But on the form doesn't submit, it just reloads retaining all information but clearing the selected image.

bradleyg commented 5 years ago

Hi @Timtech4u, I'm going to close this issue due to inactivity, but please let me know if this is still and issue and I can take a deeper look.

ProPegasus commented 3 years ago

Hi @bradleyg. I have run into the same problem as @Timtech4u. File is being uploaded fine from the Admin panel but it's not working through ModelForm My views function ef addpost(request): if not User.is_authenticated: return HttpResponse('Please Login') else: if request.method == 'POST': form = advertform(request.POST, request.FILES) if form.is_valid(): advert = form.save() advert.refresh_from_db() advert.save() return HttpResponse('Advertisement added') else: return HttpResponse('Error') else: form = advertform(initial={'soldby': request.user.userprofile.store, 'place': request.user.userprofile.town,'cell': request.user.userprofile.phone,'seller': request.user.id}) return render(request, 'adpost.html', {'form': form})

My forms.py

class advertform(forms.ModelForm): def __init__(self, *args, **kwargs): super(advertform, self).__init__(*args, **kwargs) self.fields['seller'].widget.attrs['hidden'] = True soldby = forms.CharField(widget=forms.TextInput(attrs={'readonly': 'readonly', 'class':'inpt'})) place = forms.CharField(widget=forms.TextInput(attrs={'readonly': 'readonly', 'class':'inpt'})) cell = forms.CharField(widget=forms.TextInput(attrs={'readonly': 'readonly', 'class':'inpt'})) photo = forms.URLField(widget=S3DirectWidget(dest='primay_destination')) name = forms.CharField(widget=forms.TextInput(attrs={'class': 'inpt', 'placeholder': 'Name of component'})) part_name = forms.CharField(widget=forms.TextInput(attrs={'class': 'inpt', 'placeholder': 'Part no./name'})) class Meta: model = advert fields = ('name', 'part_name', 'category', 'photo', 'soldby', 'place', 'cell', 'seller')

My model

class advert(models.Model): name = models.CharField(max_length=32) part_name = models.CharField(max_length=50, blank=True) category = models.CharField(choices=category_choices, max_length=255) photo = S3DirectField(dest='primary_destination') seller = models.ForeignKey(User, on_delete=models.CASCADE) soldby = models.CharField(max_length=32, blank=True) place = models.CharField(max_length=32, blank=True) cell = models.CharField(max_length=13,blank=True)

My HTML

`

{% csrf_token %} {{ form }}

        </form>`