1

i would like to make a model that will allow the user in the Wagtail admin site to choose a directory of images give the name of the collection in a CharField and upon a button press a collection with the given name would be created, then the images from a the given directory would then be saved to the database (so that they are avaialable in the CMS). The title of the image would be it's filename, while it's tag would be the directory name where the image is found.

I found another post on how to save Images to the database with code (image saving) but I have a problem creating collections programatically. I found here this code (from here) should work, but apparently for me it doesn't, when I do manage.py makemigrations, I get this error:

django.db.utils.IntegrityError: UNIQUE constraint failed: wagtailcore_collection.path

Do I need to provide the path in add_child? What path is it supposed to be? Thanks for any help in advance!

root_coll = Collection.get_first_root_node()
root_coll.add_child(name='testcoll')
Lazlo
  • 11
  • 3
  • Please can you show the full stack trace of the IntegrityError? I'm a bit puzzled why that would happen on the `makemigrations` step... – gasman Apr 03 '17 at 10:11
  • Please find the full error message [link](https://github.com/lazmol/wagtail-imageloader) in err_trace.log. The model.py is there, note I am really new to Wagtail so any help is welcome! – Lazlo Apr 03 '17 at 11:48
  • Just a coding related question: how does it work that Image object can be instantiated like this: Image(title="Image title", file=ImageFile(image_file, name="image-filename.jpg"))? When I have a look at [the class definition](https://github.com/wagtail/wagtail/blob/master/wagtail/wagtailimages/models.py) I don't see any __init__ method, but these (title, file) are class variables of the AbstractImage class which is inherited by Image then. So how come you can use those as arguments when instantiating Image and why is there no __init__? – Lazlo Apr 03 '17 at 11:54
  • That behaviour is provided by Django's base `Model` class, described here: https://docs.djangoproject.com/en/1.10/topics/db/models/ – gasman Apr 03 '17 at 12:48

1 Answers1

4

The problem is that you're running the code directly within the definition of GalleryPage:

class GalleryPage(Page):
    # test
    root_coll = Collection.get_first_root_node()
    root_coll.add_child(name='testcoll')

This will run every time models.py is loaded - and in particular, the ./manage.py makemigrations and ./manage.py migrate commands need to load models.py in order to find out how to set your database up. Creating database objects is naturally going to fail at that point, because the database isn't ready yet...

The best place to run test code like this is probably the ./manage.py shell command line, where you should be able to run the following lines successfully:

from wagtail.wagtailcore.models import Collection
root_coll = Collection.get_first_root_node()
root_coll.add_child(name='testcoll')
gasman
  • 23,691
  • 1
  • 38
  • 56