-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ManyToManyAdminField for use with ManyToManyField #43
base: main
Are you sure you want to change the base?
Changes from all commits
9e8ac95
0f1964b
a2d178a
1252985
9a9e448
6037158
1f79cbf
3185241
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,9 @@ def __init__(self, attr, short_description=None, admin_order_field=None, allow_t | |
|
||
super(SimpleAdminField, self).__init__(short_description, admin_order_field, allow_tags) | ||
|
||
def __name__(self): | ||
return 'SimpleAdminField' | ||
|
||
def render(self, obj): | ||
return helper.call_or_get(obj, self.attr, self.default) | ||
|
||
|
@@ -85,6 +88,39 @@ def render(self, obj): | |
return self.default | ||
|
||
|
||
class ManyToManyAdminField(SimpleAdminField): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will improve the coverage by the way. I put it together after using it myself, but I haven't had time to improve the testing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great |
||
"""Renders a ManyToManyField as links.""" | ||
|
||
def __init__(self, attr, display=None, short_description=None, admin_order_field=None, default=None): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think will be great to add a template path. to help on customization from user. then you check if template_path:
# use template
else:
# use default way |
||
self.display = display | ||
super(ManyToManyAdminField, self).__init__(attr, short_description, admin_order_field, True, default) | ||
|
||
def __name__(self): | ||
return 'ManyToManyAdminField' | ||
|
||
def render(self, obj): | ||
ref = getattr(obj, self.attr, None) | ||
display = None | ||
if self.display: | ||
display = helper.call_or_get(obj, self.display, self.default) | ||
|
||
list_str = display | ||
if hasattr(ref, 'get_queryset'): | ||
list_str = '<ul>' | ||
objects = ref.get_queryset() | ||
for obj in objects: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do think about using string template to replace this code? |
||
list_str += '<li><a href="%s">%s</a></li>' % ( | ||
reverse( | ||
admin_urlname(obj._meta, 'change'), | ||
args=(obj.pk,) | ||
), | ||
conditional_escape(display or obj) | ||
) | ||
list_str += '</ul>' | ||
|
||
return list_str | ||
|
||
|
||
class LinkChangeListAdminField(BaseAdminField): | ||
|
||
def __init__(self, app, model, attr, params=None, params_static=None, short_description=None, admin_order_field=None): | ||
|
@@ -203,7 +239,7 @@ def render(self, obj): | |
|
||
class CacheAdminField(SimpleAdminField): | ||
|
||
def __init__(self, attr, django_filter, load=None, extra=None, short_description=None, | ||
def __init__(self, attr, django_filter, load=None, extra=None, short_description=None, | ||
admin_order_field=None, allow_tags=False, default=None): | ||
self.filter = django_filter | ||
self.load = load | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you send me the warning message from django?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When used like this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about put this way:
It will work for all child classes with right name.