#2750: ManyToManyField ignores 'default' option
-------------------------------------+-------------------------------------
Reporter: bangcok_dangerus@… | Owner: Akshat
| verma
Type: Bug | Status: assigned
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 1
-------------------------------------+-------------------------------------
Changes (by Akshat verma):
* cc: Akshat verma (added)
* needs_tests: 0 => 1
* version: => 4.2
* easy: 0 => 1
* needs_docs: 0 => 1
* ui_ux: 0 => 1
Old description:
> It doesn't seem possible to use the 'default' option to pre-select values
> in the admin site for a ManyToManyField. I've tried a string value, a
> list of strings, and even (following a recommendation from someone on
> #django) passing a list of the actual objects. It would be nice to either
> have this functionality added or to update the docs to reflect the fact
> that 'default' has no effect on ManyToManyFields.
>
> -Basil
New description:
It doesn't seem possible to use the 'default' option to pre-select values
in the admin site for a ManyToManyField. I've tried a string value, a list
of strings, and even (following a recommendation from someone on #django)
passing a list of the actual objects. It would be nice to either have this
functionality added or to update the docs to reflect the fact that
'default' has no effect on ManyToManyFields.
-Basil
#you can override the init of the modelform like:
def __init__(self, *args, **kwargs):
if 'initial' not in kwargs:
kwargs['initial'] = {}
kwargs['initial'].update(product_template_admin_initial_values())
super(ProductTemplateAdminForm, self).__init__(*args, **kwargs)
#But that will not allow to use the request.
#If you depend on the request you can use the admin
get_changeform_initial_data
def get_changeform_initial_data(self, request):
return {'name': 'custom_initial_value'}
#You can call a method with the request as argument and return a
dictionary. the m2m default should be a list
--
Comment:
#you can override the init of the modelform like:
def __init__(self, *args, **kwargs):
if 'initial' not in kwargs:
kwargs['initial'] = {}
kwargs['initial'].update(product_template_admin_initial_values())
super(ProductTemplateAdminForm, self).__init__(*args, **kwargs)
#But that will not allow to use the request.
#If you depend on the request you can use the admin
get_changeform_initial_data
def get_changeform_initial_data(self, request):
return {'name': 'custom_initial_value'}
#You can call a method with the request as argument and return a
dictionary. the m2m default should be a list
--
Ticket URL: <https://code.djangoproject.com/ticket/2750#comment:26>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070187465e00a8-8aafb111-9d3b-467b-8998-a7578419dc7e-000000%40eu-central-1.amazonses.com.