Comment (by timgraham):
See also #26998 for how this applies to django-taggit.
--
Ticket URL: <https://code.djangoproject.com/ticket/12203#comment:18>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by Dmitry Mugtasimov):
If you also waiting for the fix and have a simple through model for some
reason you may use `auto_created = True` to trick Django Admin:
{{{
class JobTitleExperienceThrough(models.Model):
title = models.ForeignKey('JobTitle', on_delete=models.CASCADE,
related_name='experiences_through')
experience = models.ForeignKey('Experience', on_delete=models.CASCADE,
related_name='titles_trough')
class Meta:
# TODO(dmu) MEDIUM: Remove `auto_created = True` after these
issues are fixed:
# https://code.djangoproject.com/ticket/12203
and
# https://github.com/django/django/pull/10829
auto_created = True
class JobTitle(models.Model):
name = models.CharField(max_length=64)
role = models.ForeignKey('JobRole', on_delete=models.CASCADE,
related_name='titles')
experiences = models.ManyToManyField('Experience',
through='JobTitleExperienceThrough',
related_name='titles',
blank=True)
class Meta:
ordering = ('id',)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/12203#comment:19>
Comment (by Sylvain Fankhauser):
Unfortunately, the `auto_created=True` hack doesn't work if you want to
use foreign keys to the `through` model. You'll get a `(fields.E300) Field
defines a relation with model 'JobTitleExperienceThrough', which is either
not installed, or is abstract`.
--
Ticket URL: <https://code.djangoproject.com/ticket/12203#comment:20>
* cc: frnhr (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/12203#comment:21>
Comment (by Dmitry Mugtasimov):
For some reason I ended up with this code:
{{{
class JobTitleAdmin(admin.ModelAdmin):
def formfield_for_manytomany(self, *args, **kwargs): # pylint:
disable=arguments-differ
# TODO(dmu) MEDIUM: Remove `auto_created = True` after these
issues are fixed:
# https://code.djangoproject.com/ticket/12203
and
# https://github.com/django/django/pull/10829
# We trick Django here to avoid `./manage.py makemigrations`
produce unneeded migrations
JobTitleExperienceThrough._meta.auto_created = True # pylint:
disable=protected-access
return super().formfield_for_manytomany(*args, **kwargs)
class JobTitle(models.Model):
name = models.CharField(max_length=64)
role = models.ForeignKey('JobRole', on_delete=models.CASCADE,
related_name='titles')
experiences = models.ManyToManyField('Experience',
through='JobTitleExperienceThrough',
related_name='titles',
blank=True)
class Meta:
ordering = ('id',)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/12203#comment:22>
Comment (by Dennis):
Replying to [comment:22 Dmitry Mugtasimov]:
The trick of setting `JobTitleExperienceThrough._meta.auto_created = True`
in `formfield_for_manytomany` does indeed enable the
`ModelMultipleChoiceField` on the admin page without causing migration
issues (as far as I can see).
However, there is a dangerous side-effect of using this, in case you have
other models with a "cascading" relation directly to your explicit
through-model, e.g. `models.ForeignKey(to=JobTitleExperienceThrough,
on_delete=models.CASCADE)`:
If the initial queryset for the m2m field `JobTitle.experiences` is
filtered (e.g. as in the docs
[https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_manytomany]),
and, for whatever reason, the filter excludes some `Experience` objects
that have previously been assigned, then the corresponding records from
`JobTitleExperienceThrough` will be silently deleted, including any
records for other models pointing to them.
This means there is a serious potential for "silent" data loss.
This is because the many-to-many relation will now be updated using
`ManyRelatedManager.set()` (via `ModelAdmin.save_related()` ->
`form.save_m2m()` -> `BaseModelForm._save_m2m()` ->
`ManyToManyField.save_form_data()`).
Silent deletion could be prevented by setting `on_delete=models.PROTECT`
on any relation to the explicit through-model, but then you would first
have to be aware of the necessity.
When using a "real" auto-created `through` table (i.e. an implicit one)
the issue does not arise, because there is no way to set a `ForeignKey` to
the implicit `through`, as far as I know.
When using an inline for `JobTitleExperienceThrough`, this issue does not
occur.
--
Ticket URL: <https://code.djangoproject.com/ticket/12203#comment:23>