Opened 8 years ago

Closed 7 years ago

Last modified 5 years ago

#8903 closed (fixed)

admin.TabularInline creates duplicate pk input boxes

Reported by: Spads Owned by: Spads
Component: contrib.admin Version: 1.0
Severity: Keywords: admin TabularInline
Cc: idangazit, dgouldin@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Just filing this bug based on a conversation in #django:

18:09 <Spads> I'm converting my models and admin to django 1.0, and I'm using an admin.TabularInline. It seems to have a <td class="original"> that *repeats* the primary key input box (defaults and all). This seems to be logic for links to the "View on site" stuff, but it's an input box.
18:18 <[530]> Spads: show the code
18:24 <Spads> [530]: is the core of it
18:25 <Spads> [530]: and it's generating
18:26 <SeanL-> Spads: I think that's a bug because you have a custom pk
18:27 <SeanL-> Spads: don't know if it's been reported, just noticing myself. For me it works fine, but then I don't have a custom pk
18:27 <SeanL-> Spads: for me that first render of the pk input is the only one
18:28 <SeanL-> Spads; and it's hidden
18:28 <SeanL-> Spads: I'd file a ticket about it

Attachments (1)

10638.diff (1.8 KB) - added by dgouldin 7 years ago.

Download all attachments as: .zip

Change History (17)

comment:1 Changed 8 years ago by Spads

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

SeanL- tried testing if the pk field was editable, but that didn't fix the problem for me:

However, it feels like that's very close to the cause of this problem.

comment:2 Changed 8 years ago by seanl

I realised after some thought that even if my proposed fix had worked, there's more to the problem than that. What happens if the PK field is modified and delete is checked? Or if the PK is modified how to prevent creating a new row in the inline model's table instead of updating the existing row?

comment:3 Changed 8 years ago by gkelly

  • Component changed from to Admin interface
  • Keywords admin TabularInline added

comment:4 Changed 8 years ago by kratorius

Since dpaste is going to delete the code in the near future, here's the test case:

class NetworkInterface(models.Model):
    location = models.ForeignKey(Equipment, verbose_name="system")
    ip = models.IPAddressField("IP address", primary_key=True, default='')
    mac = models.CharField("MAC address", max_length=17, unique=True, default='02:00:00:00:00:01')
    interface_name = models.CharField(max_length=15, default="eth0")
    media_settings = models.CharField(max_length=10, blank=True, default='HURG')
    def __unicode__(self):
        return unicode(u'%s %s: %s', location, self.interface_name, self.ip)
class NetworkInterface_Inline(admin.TabularInline):
    model = models.NetworkInterface                                             
    fk_name = 'location'

class EquipmentOptions(admin.ModelAdmin):
    inlines = [NetworkInterface_Inline, PowerSupply_Inline, Memory_Inline, Processor_Inline], EquipmentOptions)


<tr class="row1 ">
   <td class="original">
     <input type="text" name="networkinterface_set-0-ip" value="" id="id_networkinterface_set-0-ip" />
   <td class="ip">
      <input type="text" name="networkinterface_set-0-ip" value="" id="id_networkinterface_set-0-ip" />
   <td class="mac">
      <input name="networkinterface_set-0-mac" value="02:00:00:00:00:01" class="vTextField" maxlength="17"
      type="text" id="id_networkinterface_set-0-mac" />
   <td class="interface_name">
      <input name="networkinterface_set-0-interface_name" value="eth0" class="vTextField" maxlength="15"
      type="text" id="id_networkinterface_set-0-interface_name" />
   <td class="media_settings">
      <input name="networkinterface_set-0-media_settings" value="HURG" class="vTextField" maxlength="10"
      type="text" id="id_networkinterface_set-0-media_settings" />
   <td class="delete"></td>

comment:5 Changed 8 years ago by idangazit

Confirming this problem, django 1.0.

comment:6 Changed 8 years ago by idangazit

  • Cc idangazit added

comment:7 Changed 8 years ago by Spads

  • Owner changed from nobody to Spads
  • Status changed from new to assigned

I've noticed that this problem still exists in django 1.0.2. Are there any plans to address it?

comment:8 Changed 8 years ago by Spads

Oh criminy, I really didn't mean to take this ticket...

comment:9 follow-up: Changed 8 years ago by Spads

As a workaround, I have been copying the
into my templates dir and applying the following ham-fisted patch. I then set
it as the template for tabular inlines in my admin where the pk is custom, but
leave the others alone.

--- /var/lib/python-support/python2.5/django/contrib/admin/templates/admin/edit_inline/tabular.html	2008-11-19 05:44:27.000000000 +0000
+++ custompktabular.html	2008-11-26 11:02:25.000000000 +0000
@@ -26,7 +26,6 @@
           {% if inline_admin_form.original %} {{ inline_admin_form.original }}{% endif %}
           {% if inline_admin_form.show_url %}<a href="../../../r/{{ inline_admin_form.original.content_type_id }}/{{ }}/">{% trans "View on site" %}</a>{% endif %}
             </p>{% endif %}
-          {{ inline_admin_form.pk_field.field }} {{ inline_admin_form.fk_field.field }}
           {% spaceless %}
           {% for fieldset in inline_admin_form %}
             {% for line in fieldset %}

comment:10 in reply to: ↑ 9 Changed 8 years ago by Spads

Replying to Spads:

--- /var/lib/python-support/python2.5/django/contrib/admin/templates/admin/edit_inline/tabular.html 2008-11-19 05:44:27.000000000 +0000
+++ custompktabular.html 2008-11-26 11:02:25.000000000 +0000
@@ -26,7 +26,6 @@

  • {{ inline_admin_form.pk_field.field }} {{ inline_admin_form.fk_field.field }}

This appears not to work. StackedInline has a similar problem here. I'm going to have to just bail and not use Inlines for this I guess.

comment:11 Changed 7 years ago by jacob

  • milestone set to 1.1
  • Triage Stage changed from Unreviewed to Accepted

comment:12 Changed 7 years ago by mtredinnick

The example in question hasn't been reduced to the simplest possible case to demonstrate the problem. Untangling the example in #10243 I came across this again. The only requirement is a manually specified, non-AutoField primary key column.

comment:13 Changed 7 years ago by kmtracey

#10638 reports this again and has a patch.

Changed 7 years ago by dgouldin

comment:14 Changed 7 years ago by dgouldin

  • Cc dgouldin@… added
  • Has patch set

Transferred my patch over from 10638.

comment:15 Changed 7 years ago by russellm

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [10670]) [1.0.X] Fixed #8903 -- Corrected rendering of admin inline formsets (tabular and stacked) when the inline model has a custom non-autofield primary key. Thanks to dgouldin for the patch.

Merge of r10666 from trunk.

comment:16 Changed 5 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

Note: See TracTickets for help on using tickets.
Back to Top