[Django] #34979: inlineformset_factory sets max_num forms to one even though there is a unique_together constraint

14 views
Skip to first unread message

Django

unread,
Nov 19, 2023, 9:54:55 PM11/19/23
to django-...@googlegroups.com
#34979: inlineformset_factory sets max_num forms to one even though there is a
unique_together constraint
-----------------------------------------+------------------------------
Reporter: ehallein | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.admin | Version: 3.2
Severity: Normal | Keywords: inline admin
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 1 |
-----------------------------------------+------------------------------
I have a unique_together primary key in one of my models. When adding an
inline admin for that model, the interface only allows adding one new
object. I have tracked it down to code in django/django/forms/models.py:
{{{
fk = _get_foreign_key(parent_model, model, fk_name=fk_name)
# enforce a max_num=1 when the foreign key to the parent model is
unique.
if fk.unique:
max_num = 1
}}}

While this makes sense for a single unique primary key, it is the wrong
behavior for a composite primary key.

Is a fix needed in Django so multiple objects with unique_together primary
keys can be added as inlines, or should this be done using custom code?

--
Ticket URL: <https://code.djangoproject.com/ticket/34979>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 19, 2023, 10:19:23 PM11/19/23
to django-...@googlegroups.com
#34979: inlineformset_factory sets max_num forms to one even though there is a
unique_together constraint
-------------------------------+--------------------------------------
Reporter: Evan Hallein | Owner: nobody

Type: Uncategorized | Status: new
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution:

Keywords: inline admin | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 1
-------------------------------+--------------------------------------
Description changed by Evan Hallein:

Old description:

> I have a unique_together primary key in one of my models. When adding an
> inline admin for that model, the interface only allows adding one new
> object. I have tracked it down to code in django/django/forms/models.py:
> {{{
> fk = _get_foreign_key(parent_model, model, fk_name=fk_name)
> # enforce a max_num=1 when the foreign key to the parent model is
> unique.
> if fk.unique:
> max_num = 1
> }}}
>
> While this makes sense for a single unique primary key, it is the wrong
> behavior for a composite primary key.
>
> Is a fix needed in Django so multiple objects with unique_together
> primary keys can be added as inlines, or should this be done using custom
> code?

New description:

I have a unique_together primary key in one of my models. When adding an
inline admin for that model, the interface only allows adding one new
object. I have tracked it down to code in django/django/forms/models.py:
{{{
fk = _get_foreign_key(parent_model, model, fk_name=fk_name)
# enforce a max_num=1 when the foreign key to the parent model is
unique.
if fk.unique:
max_num = 1
}}}

this is my model (autogenerated from an existing database):
class TrtMeasurements(models.Model):
observation = models.OneToOneField('TrtObservations',
models.DO_NOTHING, primary_key=True)
measurement_type = models.ForeignKey(TrtMeasurementTypes,
models.DO_NOTHING, db_column='measurement_type')
measurement_value = models.FloatField()
comments = models.TextField(blank=True, null=True)

class Meta:
managed = False
db_table = 'trt_measurements'
unique_together = (('observation', 'measurement_type'),)

While this makes sense for a single unique primary key, it is the wrong
behavior for a composite primary key.

Is a fix needed in Django so multiple objects with unique_together primary
keys can be added as inlines, or should this be done using custom code?

--

--
Ticket URL: <https://code.djangoproject.com/ticket/34979#comment:1>

Django

unread,
Nov 19, 2023, 10:19:52 PM11/19/23
to django-...@googlegroups.com
#34979: inlineformset_factory sets max_num forms to one even though there is a
unique_together constraint
-------------------------------+--------------------------------------
Reporter: Evan Hallein | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution:
Keywords: inline admin | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 1
-------------------------------+--------------------------------------
Description changed by Evan Hallein:

Old description:

> I have a unique_together primary key in one of my models. When adding an
> inline admin for that model, the interface only allows adding one new
> object. I have tracked it down to code in django/django/forms/models.py:
> {{{
> fk = _get_foreign_key(parent_model, model, fk_name=fk_name)
> # enforce a max_num=1 when the foreign key to the parent model is
> unique.
> if fk.unique:
> max_num = 1
> }}}
>

New description:

--

--
Ticket URL: <https://code.djangoproject.com/ticket/34979#comment:2>

Django

unread,
Nov 19, 2023, 10:48:30 PM11/19/23
to django-...@googlegroups.com
#34979: inlineformset_factory sets max_num forms to one even though there is a
unique_together constraint
-------------------------------+--------------------------------------
Reporter: Evan Hallein | Owner: nobody
Type: Uncategorized | Status: closed
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution: fixed

Keywords: inline admin | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 1
-------------------------------+--------------------------------------
Changes (by Evan Hallein):

* status: new => closed
* resolution: => fixed


Comment:

fixed by changing the model to not use the onetoone as a primary key and
instead have a separate primary key:

{{{
class TrtMeasurements(models.Model):
id = models.AutoField(primary_key=True)
observation = models.ForeignKey('TrtObservations',
on_delete=models.CASCADE)
measurement_type = models.ForeignKey(TrtMeasurementTypes,
on_delete=models.CASCADE, db_column='measurement_type')


measurement_value = models.FloatField()
comments = models.TextField(blank=True, null=True)

class Meta:
managed = False
db_table = 'trt_measurements'
unique_together = (('observation', 'measurement_type'),)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34979#comment:3>

Django

unread,
Nov 19, 2023, 11:28:51 PM11/19/23
to django-...@googlegroups.com
#34979: inlineformset_factory sets max_num forms to one even though there is a
unique_together constraint
-------------------------------+--------------------------------------
Reporter: Evan Hallein | Owner: nobody
Type: Uncategorized | Status: closed
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution: invalid

Keywords: inline admin | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 1
-------------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* resolution: fixed => invalid


--
Ticket URL: <https://code.djangoproject.com/ticket/34979#comment:4>

Reply all
Reply to author
Forward
0 new messages