Hi, all.
I was not sure if this question was better suited to the users or
developers mailing list and posted in both. Feel free to delete one of them
if you see fit.
I have a Submission model with a many-to-many relationship with a Market
model and explicitly defined a SubmissionMarket model as intermediary
model. I have defined
Submission inlines in a ModelAdmin class (used by Django admin) and defined
the markets field as the first one in the fields attribute of a form
associated with that submission inline. However, that field is being shown
by last (at the end of the inline row).
Debugging, I found out that:
- django.forms.models.fields_for_model() creates a dict of form fields for
a Django model. That function is used during the construction of any form.
- a form field defined in the Form.Meta.fields is ignored in
fields_for_model() if formfield_callback is passed as argument and if that
callable returns None
- django.forms.models.ModelFormMetaclass
passes BaseModelAdmin.formfield_for_dbfield as formfield_callback arg in
fields_for_model()
- This formfield_for_dbfield() method ends up
calling formfield_for_manytomany() if the field is m2m. The latter method
returns None if the intermediary model was not created automatically by
Django, which causes the field to be ignored. This is the code that does it
in django/contrib/admin/options.py:
# If it uses an intermediary model that isn't auto created, don't
show
# a field in admin.
if not db_field.remote_field.through._meta.auto_created:
return None
- Later, the markets field is added at the end of the form, I haven't
investigated why.
My question is: what's the motivation of ignoring the m2m field if the
intermediary model is not auto created? Could this behavior be customized?
Thanks in advance!
Regards,
Alan
--
You received this message because you are subscribed to the Google Groups
"Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-developers/d8db85d2-91ca-4779-8316-638ebc1f07dan%40googlegroups.com.