I'm adding a family tree manager in wagtail admin ModelAdmin, the structure is something like this:
Clients/
Management
Family Tree/
Person/
So I need to make sure that every family tree and Person (members of the family tree) are available just to the user that input the data (and the admin).
It's my first time with wagtail, I just read the docs but any advice is welcome :)
Filtering on the model admin view
On your ModelAdmin class, you should define
get_querysetto filter the items shown on the list view as per your need.However it would only not display the items, a user would still be able to access other items by modifying the URL. To prevent that, you'll want to define a
permission_helper_classand set theuser_can_inspect,user_can_create,user_can_editanduser_can_deletemethods to return True/False accordingly.Assigning the user to the created object
Based on your comment below, let's assume you have the following model definition:
Note that
null=Falsewill fail if you already have some entries in the database. If that's the case, you'll have to create some custom migrations.In order to assign the user creating an object with the object itself, you'll have to override the
CreateViewof the model admin with a custom one.Note that this way, it will output a hidden
managed_byform field which you later set to the correct value. If that's an issue for you, you would have to exclude the field and then overwrite theform_validmethod. I chose not to because you would have to overwrite the method completely (and experience shows that a given update of Wagtail will differ from your copied implementation and you won't notice), not just override/extend it as the initial method callsform.save()which would fail as the requiredmanaged_byfield is missing.And then set this view on your model admin: