Opened 15 years ago

Last modified 10 years ago

#13794 closed Bug

Django does not respect to_field's model on an inline model admin — at Version 9

Reported by: sebastien@… Owned by: gautier
Component: Forms Version: dev
Severity: Normal Keywords: admin inline to_field formset
Cc: ghayoun@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Aymeric Augustin)

The problem occurs in the function __unicode__ of ModelB

When editing a ModelA instance in admin site, all ModelB instances have a modela_id set to the pk of the ModelA instance, instead of the value of the to_field's field. And, when accessing to the field modela of a ModelB instance, a DoesNotExist exception is raised.

This problem does not occur in the shell :

In [3]: ModelA.objects.all()[0].modelb_set.all()[0].modela_id
Out[3]: u'TRUC'

In [6]: ModelB.objects.all()[0].modela_id
Out[6]: u'TRUC'

See below to reproduce

# models.py

class ModelA(models.Model):
    code = models.CharField(max_length=20, unique=True)

class ModelB(models.Model):
    modela = models.ForeignKey(ModelA, to_field="code")
    position = models.IntegerField()

    def __unicode__(self):
        return u"%s" % self.modela

# admin.py

class ModelBInlineAdmin(admin.TabularInline):
    model = ModelB

class ModelAAdmin(admin.ModelAdmin):
    model = ModelA
    inlines = [ModelBInlineAdmin]

admin.site.register(ModelA, ModelAAdmin)

Change History (12)

comment:1 by anonymous, 14 years ago

Cc: ghayoun@… added

comment:2 by gautier, 14 years ago

Has patch: set
Keywords: formset added
Owner: changed from nobody to gautier
Status: newassigned
Triage Stage: UnreviewedAccepted

Same cause as #10243, #11043, fixed by [10756].

But the merging of soc2009/model-validation in [12098] broke the fix, by forcing the value of the related primary key into the foreign key field.

My patch removes this behaviour, and add a regression test. All other unit tests still pass.

by gautier, 14 years ago

Fix + regression test

comment:3 by Julien Phalip, 14 years ago

Component: django.contrib.adminForms

comment:4 by Graham King, 14 years ago

Severity: Normal
Type: Bug

comment:5 by patchhammer, 14 years ago

Easy pickings: unset
Patch needs improvement: set

fix_inline_model_with_to_field.diff fails to apply cleanly on to trunk

by gautier, 14 years ago

Attachment: ticket13794.patch added

Fix + regression test, updated for new trunk

comment:6 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

by anonymous, 12 years ago

Attachment: to_field_formsets.patch added

comment:7 by anonymous, 12 years ago

Patch needs improvement: unset
Version: 1.21.5-beta-1

I updated the patch so that is does not change behavior at all for no to_field formsets. Any feedback or suggestions welcome.

This bug has been present for a while and would be nice to get fixed.

comment:8 by anonymous, 12 years ago

Status: assignednew

comment:9 by Aymeric Augustin, 12 years ago

Description: modified (diff)

Added a bit of markup for reaadability.

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