* cc: chris+django@… (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/2750#comment:15>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* cc: anubhav9042@… (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/2750#comment:16>
Comment (by jarshwah):
https://github.com/django/django/pull/3108
I'm of the opinion that `default` for ManyToManyField doesn't make sense.
The default value of a field is used when a model is saved without a user
value. M2M fields can not have a value when being saved (values must be
added with the RelatedManager), so the default value would always be
applied if implemented.
I think the underlying problem of pre-selecting values in the admin for
m2m form fields is orthogonal to whether or not m2m model fields should
respect the default option.
--
Ticket URL: <https://code.djangoproject.com/ticket/2750#comment:17>
* has_patch: 0 => 1
* stage: Someday/Maybe => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/2750#comment:18>
* cc: josh.smeaton@… (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/2750#comment:19>
* needs_better_patch: 0 => 1
Comment:
There are failing tests and usage of this in Django's test suite that need
to be removed.
--
Ticket URL: <https://code.djangoproject.com/ticket/2750#comment:20>
* has_patch: 1 => 0
Comment:
While I don't think `default` makes sense for m2m models, it appears that
forms make use of the default option, so we can't simply check/warn and
document. This will involve some deeper investigation with regards to how
forms use the m2m default. I'll try to look into this a little more.
--
Ticket URL: <https://code.djangoproject.com/ticket/2750#comment:21>
Comment (by phildini):
Has there been any movement on this? When you say forms make use of the
default option, what do you mean?
--
Ticket URL: <https://code.djangoproject.com/ticket/2750#comment:22>
Comment (by François Granade):
Replying to [comment:22 Philip James]:
> Has there been any movement on this? When you say forms make use of the
default option, what do you mean?
what is meant here is:
the `default` option for a `ManyToManyField` is not used when saving the
object (when calling `<object>.save()`, or other): even if it is set (e.g.
to a callable returning an object list), the field will be set to an empty
list when saving.
However, this `default` option value is used in Django `Form` set up to
edit this object. In particular, in Django Admin, a the multivalue drop-
down will include for the field, that will contain all the objects
returned by the callable defined as the `default` value - and they will be
all selected. If the user saves the object without any edit (by pushing
the form's "save" button), then the field will indeed be set to all the
values.
The current behavior is a little misleading, I think (and I'm not the
only. one, from previous comments): I would expect the options set if the
model's fields to be used in the model's methods; if an option is only
used for forms, I would expect it to be set in the form, not in the model.
There may be design reasons for the current behavior though ?
In any case, this should probably be documented in the `ManyToManyField`
documentation ?
--
Ticket URL: <https://code.djangoproject.com/ticket/2750#comment:23>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/2750#comment:24>
* owner: nobody => Akshat verma
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/2750#comment:25>
* 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>
Comment (by Mariusz Felisiak):
Akshat, I deleted your comment to restore the ticket description. Please
don't change ticket descriptions. If you want to work on the ticket and
propose a patch, do this via GitHub PR.
* owner: (none) => Akshat verma
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/2750#comment:28>