Re: [Django] #2750: ManyToManyField ignores 'default' option

13 views
Skip to first unread message

Django

unread,
Feb 22, 2012, 6:42:34 AM2/22/12
to django-...@googlegroups.com
#2750: ManyToManyField ignores 'default' option
-------------------------------------+-------------------------------------
Reporter: bangcok_dangerus@… | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version:
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by gcc):

* 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.

Django

unread,
Dec 19, 2013, 3:17:35 PM12/19/13
to django-...@googlegroups.com
#2750: ManyToManyField ignores 'default' option
-------------------------------------+-------------------------------------
Reporter: bangcok_dangerus@… | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version:
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by anubhav9042):

* cc: anubhav9042@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/2750#comment:16>

Django

unread,
Aug 23, 2014, 6:30:04 AM8/23/14
to django-...@googlegroups.com
#2750: ManyToManyField ignores 'default' option
-------------------------------------+-------------------------------------
Reporter: bangcok_dangerus@… | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version:
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Aug 23, 2014, 6:30:41 AM8/23/14
to django-...@googlegroups.com
#2750: ManyToManyField ignores 'default' option
-------------------------------------+-------------------------------------
Reporter: bangcok_dangerus@… | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version:
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by jarshwah):

* has_patch: 0 => 1
* stage: Someday/Maybe => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/2750#comment:18>

Django

unread,
Aug 23, 2014, 6:31:19 AM8/23/14
to django-...@googlegroups.com
#2750: ManyToManyField ignores 'default' option
-------------------------------------+-------------------------------------
Reporter: bangcok_dangerus@… | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version:
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by jarshwah):

* cc: josh.smeaton@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/2750#comment:19>

Django

unread,
Aug 23, 2014, 7:43:03 PM8/23/14
to django-...@googlegroups.com
#2750: ManyToManyField ignores 'default' option
-------------------------------------+-------------------------------------
Reporter: bangcok_dangerus@… | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version:
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 1

Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by timgraham):

* 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>

Django

unread,
Aug 23, 2014, 8:38:47 PM8/23/14
to django-...@googlegroups.com
#2750: ManyToManyField ignores 'default' option
-------------------------------------+-------------------------------------
Reporter: bangcok_dangerus@… | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version:
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 1

Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by jarshwah):

* 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>

Django

unread,
Jun 2, 2016, 3:11:44 PM6/2/16
to django-...@googlegroups.com
#2750: ManyToManyField ignores 'default' option
-------------------------------------+-------------------------------------
Reporter: bangcok_dangerus@… | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Dec 28, 2017, 11:42:51 AM12/28/17
to django-...@googlegroups.com
#2750: ManyToManyField ignores 'default' option
-------------------------------------+-------------------------------------
Reporter: bangcok_dangerus@… | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Feb 22, 2021, 3:29:40 PM2/22/21
to django-...@googlegroups.com
#2750: ManyToManyField ignores 'default' option
-------------------------------------+-------------------------------------
Reporter: bangcok_dangerus@… | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/2750#comment:24>

Django

unread,
Apr 3, 2023, 4:56:55 AM4/3/23
to django-...@googlegroups.com
#2750: ManyToManyField ignores 'default' option
-------------------------------------+-------------------------------------
Reporter: bangcok_dangerus@… | Owner: Akshat
| verma
Type: Bug | Status: assigned

Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Akshat verma):

* owner: nobody => Akshat verma
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/2750#comment:25>

Django

unread,
Apr 3, 2023, 5:06:21 AM4/3/23
to django-...@googlegroups.com
#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

unread,
Apr 3, 2023, 5:11:07 AM4/3/23
to django-...@googlegroups.com
#2750: ManyToManyField ignores 'default' option
-------------------------------------+-------------------------------------
Reporter: bangcok_dangerus@… | Owner: Akshat
| verma
Type: Bug | Status: assigned
Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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.

Django

unread,
Apr 3, 2023, 7:07:02 AM4/3/23
to django-...@googlegroups.com
#2750: ManyToManyField ignores 'default' option
-------------------------------------+-------------------------------------
Reporter: bangcok_dangerus@… | Owner: Akshat
| verma
Type: Bug | Status: assigned
Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Akshat verma):

* owner: (none) => Akshat verma


* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/2750#comment:28>

Reply all
Reply to author
Forward
0 new messages