-1

I saw many tutorials on how to create a File upload field / Image Field using forms and templates. But I have a model called TestModel and I want to manage it with Django's default admin panel, how can I add a upload field to it ?

class TestModel(models.Model):
    cover = models.ImageField(upload_to='covers')

when I try makemigrations, result will be :

You are trying to add a non-nullable field 'cover' to testmodel without a default; we can't do that ( the database needs something to populate existing rows). Please select a fix: 1) Provide a one-off default now (will be set on all existing rows) 2) Quit, and let me add a default in models.py Select an option:

What it the problem ?

Uncle
  • 133
  • 1
  • 10
  • 2
    Provide a one-off default or add a default in models.py – Sayse May 17 '16 at 08:33
  • I tried with default=None but still the same result. – Uncle May 17 '16 at 08:34
  • @Sayse what should I set as default value ? – Uncle May 17 '16 at 08:39
  • 1
    [Very first google result for "You are trying to add a non-nullable field"](http://stackoverflow.com/questions/26185687/you-are-trying-to-add-a-non-nullable-field-new-field-to-userprofile-without-a). Please try to research your problem – Sayse May 17 '16 at 08:48
  • @Sayse it is for char field ! what is default for Image Field ? – Uncle May 17 '16 at 08:51
  • No, the accepted answer uses charfield but there are other answers in there that show other solutions. if not that then adding "imagefield" to the previously mentioned google result shows a page with a default. For further reference, see [ask] and [Is showing effort needed?](http://meta.stackoverflow.com/questions/288176/is-showing-effort-needed) – Sayse May 17 '16 at 08:55
  • 1
    @Sayse Thank You, I found the solution. – Uncle May 17 '16 at 11:10

1 Answers1

0

It's coming because of you did not put default value for cover field. You have two option

1-a. It asking for default value ,select 1 and put dafault value, Ex-0,1

1-b.You can also select option 2 and change in your model field. https://docs.djangoproject.com/en/1.9/ref/models/fields/#imagefield

  1. Clear testmodel table and then migrate.
Rahul Pandey
  • 129
  • 6