1

I'm new to Django and Wagtail and trying to set my CMS up to load with the minimum set of pages for our nav to function correctly. The short problem is that objects extending Wagtail's Page type are not loading, though the data migration appears to succeed.

In my case I have a bunch of Organizations & OrganizationTypes (extending BasicObject), as well as some Page objects: OrganizationIndexPages OrganizationPages. I've dumped the data using the standard dumpdata command and have it, including all pages, in an initial_data.json file and am loading it using a data migration similar to the accepted answer here. The problem is that I get all my Organizations and OrganizationTypes, but no Page objects after re-migrating. I even added some printing to the output to verify:

loaded <DeserializedObject: orgs.Organization(pk=19)>
loaded <DeserializedObject: orgs.Organization(pk=20)>
loaded <DeserializedObject: orgs.OrganizationType(pk=1)>
loaded <DeserializedObject: orgs.OrganizationType(pk=2)>
loaded <DeserializedObject: orgs.OrganizationType(pk=3)>
loaded <DeserializedObject: orgs.OrganizationPage(pk=7)>
loaded <DeserializedObject: orgs.OrganizationPage(pk=8)>
loaded <DeserializedObject: orgs.OrganizationPage(pk=10)>

edit: I've noticed that in my dumped json that some fields that are probably required are missing from my pages. I'm just calling save() on my deserialized json objects. However, if I drop into the database those missing rows are there- I can't even create a new page for an organization in the wagtail interface dut to a OneToOneField relationship... but they don't appear in the interface or in the shell using OrganizationPage.objects.all() (I'm new to everything so maybe not using the shell right either).

Here is an example initial json and its corresponding Page model:

# Example json
{
  "model": "orgs.organizationpage",
  "pk": 21,
  "fields": {
    "org": 14,
    "body": "[{\"type\": \"paragraph\", \"value\": \"<p>Here is the body</p>\", \"id\": \"804642df-31f8-4e17-b114-cb05c4b3e265\"}]"
  }
},

# Page class
class OrganizationPage(Page):
    org = models.OneToOneField('orgs.Organization', on_delete=models.PROTECT)
    body = StreamField([
        ('heading', HeadingBlock(classname="full title")),
        ('paragraph', blocks.RichTextBlock()),
        ('image', ImageChooserBlock()),
        ('table', TableBlock()),
    ])

    api_fields = [
        APIField('org'),
        APIField('body'),
    ]
    content_panels = Page.content_panels + [
        StreamFieldPanel('body'),
    ]
    search_fields = Page.search_fields + [
        SearchField('body'),
    ]
    settings_panels = Page.settings_panels + [
        FieldPanel('org'),
    ]

# load_fixture function from migration
def load_fixture(apps, schema_editor):
    fixture_file = os.path.join(fixture_dir, fixture_filename)
    fixture = open(fixture_file, 'rb')
    objects = serializers.deserialize('json', fixture, ignorenonexistent=True)
    for obj in objects:
        obj.save()
        print("loaded {}".format(obj))
    fixture.close()
erikdstock
  • 1,117
  • 9
  • 16
  • "some fields that are probably required are missing from my pages" - which ones? – gasman Oct 09 '18 at 14:34
  • @gasman- updating with more description. I don't *see* anything there that is obviously missing, but I know there is a `slug` under the promote tab. – erikdstock Oct 09 '18 at 14:58
  • Your fixture JSON should contain a `wagtailcore.page` entry containing the standard fields such as `slug`, alongside the `orgs.organizationpage` record. What's the exact `dumpdata` command you're using? – gasman Oct 09 '18 at 15:42
  • `python manage.py dumpdata --indent=2 orgs > ./backend/apps/orgs/fixtures/initial_data.json` – erikdstock Oct 10 '18 at 01:36
  • Alright, with some googling and looking at the docs I've made some changes that I *think* are close to working: 1. Dump data with `python manage.py dumpdata --natural-foreign --indent=2 wagtailcore.Page orgs > ./backend/apps/orgs/fixtures/initial_data.json`. 2. *manually delete* `wagtailcore.page` entries in the json not related to orgs (maybe i could do this as part of my dumping script?). 3. load fixtures that include both custom pages and wagtailcore.pages in a data migration. ...Does this sound correct? I might write it up as an answer if so but step 2 feels like a smell to me. – erikdstock Oct 10 '18 at 15:04
  • Deleting pages manually from a fixture is risky - the tree structure is defined by the `path` and `numchild` fields, and your fixture must contain a complete consistent page tree or else you'll run into integrity errors later. It's doable as long as you update `numchild` appropriately and don't remove any pages that are ancestors of pages you want to keep (which probably means you need to include at least your homepage model in the `dumpdata` command too). It's probably safer to delete your unwanted pages in the Wagtail admin before running dumpdata. – gasman Oct 10 '18 at 15:48
  • Intersting. Thanks. Once I have a durable solution I'll update with my answer here- would you mind approving it then @gasman? – erikdstock Oct 10 '18 at 19:17

0 Answers0