question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Documentation request: more `modeladmin` examples

See original GitHub issue

It’s great that modeladmin got included in 1.5, but it would be great to see some more documentation, particularly in the form of examples. We’re trying to do something relatively simple - make one of our model fields hidden and auto-populated from request.user - but can’t figure out the “canonical” method (or even one that works satisfactorily).

So far we’ve tried:

  1. Override create_view_class and edit_view_class on our ModelAdmin subclass to views that reference a custom form where the field widget is overridden. This works, but the label is still displayed in the view.
  2. Override create_view_class and edit_view_class on our ModelAdmin subclass to views that reference a custom form where excludes lists our field. This raises an error somewhere in the guts of Wagtail that expects that field to be present.
  3. Similar to 1, override the views, override get_initial to return information from request.user into the form, and define panels on the models with a FieldPanel('user', widget=HiddenInput()) which has a similar issue to 1 - it works, but the field label is still visible on the form.
  4. Progressing from 3, remove the field from panels completely - this successfully removes the field from view (and from the form) but then we can’t access request in order to auto-populate the value.

I’m not necessarily asking for the right answer for how to achieve what we need, more I wanted to point out that though having modeladmin is great, the indication in the docs that “the ModelAdmin class has a large number of methods that you can override or extend, allowing you to customise the behaviour”, while true, has led us down several blind alleys in trying to achieve something pretty basic.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ababiccommented, Jun 16, 2016

Hey @jimr. I would consider the label still showing for a hidden field as a bug, yes, and probably worth raising as it’s own issue, if you wouldn’t mind? I believe it’s a ‘panels’ thing, though, rather than a modeladmin-specific one.

I definitely agree with you about getting some guidance into the docs regarding overriding of model forms. If issues like #2717 and this are already cropping up, then it’s clear that more people will run into similar difficulties in future. I guess it’s a double-edged sword when you use common Django components. There are a lot of methods there that developers will be used to extending/overriding, which I would hope makes their lives easier in the main. But, Wagtail has it’s ‘edit handler’ layer that complicates things when it comes to forms. I can’t claim to know the full ins and outs of that myself, but @gasman may be able to offer some guidance about how we might possibly work around that in future.

An important thing to bare in mind here, is that modeladmin is still only handling the adding and editing for non-page models at this stage. That’s mostly why the customisation available on the ModelAdmin class itself is focussed around ‘listing’ behaviour (that is the only view that is used consistently). In the future, I’d hope for the page-management views and other things in core to become class-based; which would make it possible for modeladmin to extend them, and offer a more consistent level of control for everything.

0reactions
gasmancommented, Nov 10, 2016

Now addressed by #2753 / #2902.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Django admin site
ModelAdmin options¶ · A callable that accepts one argument, the model instance. For example: @ display(description='Name') def upper_case_name(obj): return ( · A ...
Read more >
ModelAdmin — Wagtail Documentation 4.1.1 documentation
The modeladmin module allows you to add any model in your project to the Wagtail admin. You can create customisable listing pages for...
Read more >
Django Tutorial Part 4: Django admin site - MDN Web Docs
First we'll show you how to register the models with the admin site, then we'll show you how to login and create some...
Read more >
Customize the Django Admin With Python
To get the most out of this tutorial, you'll need some familiarity with Django, ... The example here shows what you can do...
Read more >
Modifying a ModelAdmin instance at runtime - Google Groups
The documentation of ModelAdmin.get_form()[1] demonstrates modifying self.exclude in order to modify the add/change form on a per-request basis.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found