django - GeoDjango: Can I use OSMGeoAdmin in an Inline in the User Admin? -
profile contains pointfield. i've used osmgeoadmin in profileadmin, here:
class profileadmin(admin.osmgeoadmin): model = profile but can't figure out how use in inline display in useradmin. have set below:
# user admin, profile attached class profileinline(admin.stackedinline): model = profile can_delete = false verbose_name_plural = 'profile' # 1 displayed in view class useradmin(useradmin): inlines = ( profileinline, ) admin.site.unregister(user) admin.site.register(user, useradmin) is possible use class osmgeoadmin in situation?
this feature request guess.
as workaround, can take advantage of fact inlinemodeladmin quite similar modeladmin. both extend basemodeladmin.
inheriting both stackedinline , modeladmin should not clash much.
the issue both __init__() methods take 2 positional arguments , call super().__init__() without arguments. whatever inheritance order, fail typeerror: __init__() missing 2 required positional arguments: 'parent_model' , 'admin_site'
fortunately, inlinemodeladmin.__init__() method, 1 interested in, not verbose nor complex (not super().__init__() calls in cascade).
here looks in django 1.9:
def __init__(self, parent_model, admin_site): self.admin_site = admin_site self.parent_model = parent_model self.opts = self.model._meta self.has_registered_model = admin_site.is_registered(self.model) super(inlinemodeladmin, self).__init__() if self.verbose_name none: self.verbose_name = self.model._meta.verbose_name if self.verbose_name_plural none: self.verbose_name_plural = self.model._meta.verbose_name_plural and here parent (basemodeladmin) looks in django 1.9
def __init__(self): overrides = formfield_for_dbfield_defaults.copy() overrides.update(self.formfield_overrides) self.formfield_overrides = overrides now let's put together:
from django.contrib.admin.options import formfield_for_dbfield_defaults # user admin, profile attached class profileinline(osmgeoadmin, admin.stackedinline): model = profile can_delete = false verbose_name_plural = 'profile' # 1 displayed in view def __init__(self, parent_model, admin_site): self.admin_site = admin_site self.parent_model = parent_model self.opts = self.model._meta self.has_registered_model = admin_site.is_registered(self.model) overrides = formfield_for_dbfield_defaults.copy() overrides.update(self.formfield_overrides) self.formfield_overrides = overrides if self.verbose_name none: self.verbose_name = self.model._meta.verbose_name if self.verbose_name_plural none: self.verbose_name_plural = self.model._meta.verbose_name_plural class useradmin(useradmin): inlines = ( profileinline, ) admin.site.unregister(user) admin.site.register(user, useradmin) it's not satisfying solution requires copy/paste code django, may different within version of django use, , might pain maintain when upgrading django. should work until included in django mix-in or inlinemodeladmin.
note: code snippets above taken django 1.9, should browse github tags find snippets corresponding version.
Comments
Post a Comment