0

I want a default picture in the model file field, my code is not working. The file field is empty.

file = models.FileField(upload_to='team_icons', null='True', blank='True', 
default='pictures/picture.png')
Sander
  • 51
  • 6
  • Does this answer your question? [Django FileField default file](https://stackoverflow.com/questions/6740715/django-filefield-default-file) – Muteshi Dec 19 '20 at 21:02

2 Answers2

0

Use ImageField for storing images in objects and FileField to save them.

in your model:

class YourModel(models.Model):
    image = models.ImageField(upload_tp='team_icons', null=True, blank=True)

if your form:

class YourForm(forms.ModelForm):
    image = models.FileField()

    class Meta:
        model = YourModel
        fields = '__all__'
SLDem
  • 2,065
  • 1
  • 8
  • 28
  • i understand, but is it possible to realise this directly in de model field. – Sander Dec 19 '20 at 20:54
  • Why would you want to do that? If you want to store an image there's a perfectly working field for it, just use it. – SLDem Dec 19 '20 at 20:57
  • because i dont want to rewrite the app. I can handle it in swift but i would be nicer if there was an easy way in django. – Sander Dec 19 '20 at 21:40
  • It wont take long, just change the field and migrate. By the way it could be that your `null='True', blank='True'` are causing an error because these arguments don't accept parameters in braces but boolean values, i.e. you should write it like `null=True, blank=True` – SLDem Dec 19 '20 at 21:52
0

You need to add the default image with settings.MEDIA_ROOT base path. MEDIA_ROOT is absolute filesystem path to the directory that will hold user-uploaded files.

file = models.FileField(upload_to='team_icons', null='True', blank='True', 
default='settings.MEDIA_ROOT/pictures/picture.png')

Also make sure that image exist at that path.

Sohaib
  • 566
  • 6
  • 15