ModelAdmin popup to add related item

463 views
Skip to first unread message

Robert Slotboom

unread,
Aug 28, 2016, 10:19:09 AM8/28/16
to Wagtail support
Hi guys,

I have created some non-page models and integrated them in Wagtail using ModelAdmin.
Some models however have a foreign key to other non-page models.

When creating items, the related items have to be added first.

In Django Admin however it is possible to add a related item in case it does not exists.
In stead of selecting it from the list, clicking the plus sign next to it reveals a popup to enter the data.
When done, the list is updated and the fresh item can be selected.

Can this be implemented in Wagtail?

Cheers,

Robert



Robert Slotboom

unread,
Sep 1, 2016, 11:31:52 AM9/1/16
to Wagtail support
Any thoughts?

Andrew Barton

unread,
Sep 1, 2016, 4:05:43 PM9/1/16
to Wagtail support
I'm not sure how it works in Django Admin. I'd try looking at the source code to see how it's done.

However, the Wagtail solution (I think) was to create ModelCluster https://github.com/torchbox/django-modelcluster

My thoughts :)

Robert Slotboom

unread,
Sep 3, 2016, 3:04:38 PM9/3/16
to Wagtail support
Hi Andrew,

Many thanks but unfortunately...

I guess I’ve to look how image and document choosers are implemented.

Cheers,

Robert

David

unread,
Sep 9, 2016, 11:15:09 AM9/9/16
to Wagtail support
If you do manage this please share back - It would be very helpful. So would support for inline forms/related objects.

Edd

unread,
Sep 15, 2016, 6:24:16 PM9/15/16
to Wagtail support
Hi Robert,

If I've understood what you're looking for there isn't a hugely simple way to do what you're describing in Wagtail. Though you could populate non-page models from other page-models in a slightly less accessible way e.g.

class SubgenreClass(ClusterableModel):

    title = models.CharField(max_length=255, help_text="Be as esoteric as you'd like")
    description = models.TextField(blank=True, help_text='A description of the sub-genre')

    panels = [
            FieldPanel('title'),
            FieldPanel('description')
    ]


class SubGenreRelationship(Orderable, SubgenreClass):
        subgenre_in_editor = ParentalKey('GenreClass', related_name='sub_genre_relationship')


class GenreClass(ClusterableModel):
    title = models.CharField("The genre", max_length=254, help_text='The genre. Something high level e.g. pop, metal, punk etc')
    slug = models.SlugField(
        allow_unicode=True,
        max_length=255,
        help_text="The name of the page as it will appear in URLs e.g http://domain.com/blog/[my-slug]/",
    )
    genre_description = RichTextField(blank=True, help_text='A description of the genre')

    panels = [
        FieldPanel('title'),
        FieldPanel('slug'),
        FieldPanel('genre_description'),
        InlinePanel('sub_genre_relationship', label="Subgenre", help_text="Note: this subgenres will populate the sub-genre model", min_num=1)
    ]

This is from a work-in-progress site, and specifically the model at https://github.com/heymonkeyriot/rocknroll-wagtail/blob/master/monkeywagtail/genre/models.py but gives the idea of what you're looking for I think (you can clone the repo and check if you want to see it in practice)

SubgenreClass is populated by GenreClass using  the related name sub_genre_relationship.

This works fine to the extent that it allows a class to be populated if there's a one-to-many relationship (e.g. a single genre has lots of subgenres). It means you can access either the subgenre or genre model elsewhere in your application, but the subgenre model can't be made accessible through modeladmin without causing some confusion (because there's no way to define the relationship on the subgenre model back to the parent genre). That suited my use case for the project, and hopefully even if it doesn't suit yours will get you a little closer to find the solution. Would love to hear if you come up with something that gets closer to mimicking the Django admin UI for adding related content via a modal on the parent item.

cheers
edd

Robert Slotboom

unread,
Sep 17, 2016, 5:18:48 AM9/17/16
to Wagtail support
Hi Edd,

At the moment your approach seems the only way to accomplish what I want.
And actually it forced me to think again about my model design.

This is what I got:
A generic activity model and three derived models: lecture, course and tour.
A generic planning model and three derived models: lecture_planning, course_planning and tour_planning.

The derived ones have a ModelAdmin

Maybe I should change this in:

class Activity(ClusterableModel)

class Planning(ClusterableModel)

class PlanningMeeting(models.Model):
    planning
= ParentalKey(

       
'cms.Planning',
        related_name
='planning_meetings',
   
)

class Lecture(Activity):
    planning_panels
= [
       
InlinePanel('lecture_plannings', max_num=1)

   
]

class LecturePlanning(Planning):
    general_panels
= [
       
InlinePanel(
           
'planning_meetings',
            max_num
= 1,
       
)
   
]


I’ll let you know the outcome.

Thanks,

Robert

Robert Slotboom

unread,
Sep 17, 2016, 7:55:14 AM9/17/16
to Wagtail support
Hi Edd,


The outcome is Hmmmmmmm..
Nested Inlines are not supported so I can’t use Activity > Planning > Meeting

Have to dig a bit further.

Thanks anyway,

Robert
Reply all
Reply to author
Forward
0 new messages