0

models.py

class Report(models.Model):
    user = models.ForeignKey(User, null=False)
    photo_files_attached = models.BooleanField('Photos', default=False)

forms.py

class MediaForm(forms.ModelForm):
    photo_files_attached = forms.FileField(label='Choose a file')
    class Meta:
        model = Report
        field = ['photo_files_attached']

views.py

def media(request):
    user = request.user  
    try:
        report = Report.objects.get(user=user.id)
    except:
        report = None
    mediaForm = MediaForm()
    if request.method =='POST':
        mediaForm = MediaForm(request.POST,request.FILES)
        if mediaForm.is_valid():
            media = Report(photo_files_attached = request.FILES['photo_files_attached'])
            media.save()


    return render(request, 'media.html',
                  {

                   'mediaForm':MediaForm,

                 })

I am trying to upload a image file through django and save it in database.Once the file is saved ,the images are get collecetd in media folder in project.But it is completely not working.May i know what i did wrong.

user2086641
  • 4,331
  • 13
  • 56
  • 96

1 Answers1

3

As far as I can see you don't have a single FileField on your Model so where do you expect any uploaded files to be bound to your object?

You should either create your own saving logic in within the form.is_valid() scope or use a different approach overall.

Here's a single file example, using a modelform, update forms.FileField on django forms

Community
  • 1
  • 1
Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
  • I am using BooleanField,so that,if a file uploaded and save in database it should be saved as '1' if the file is uploaded sucessfully else it should be always '0'.It is actually a small module for me to do,since the db is already designed i con't do any alteration.Any idea how to proceed this further. – user2086641 May 18 '13 at 13:21
  • Any idea in which way we can perform,please suggest me,have any reference file please share with me.Thanks – user2086641 May 18 '13 at 13:38
  • Why can't you alter the database with a schemamigration tool like [south](http://south.aeracode.org/)? – Hedde van der Heide May 18 '13 at 13:48
  • You can save the files manually to the filesystem and create new filenames for each using the primary_key of the user and the report pk as a prefix followed by a datetime stamp to avoid duplicates but its really messy you should really schemigrate to do this properly – Hedde van der Heide May 18 '13 at 13:54
  • I tried in different method ,getting some error i updated my question in this link http://stackoverflow.com/questions/16625055/nonetype-object-has-no-attribute-user – user2086641 May 18 '13 at 13:57