Opened 16 years ago
Last modified 11 years ago
#13794 closed Bug
Django does not respect to_field's model on an inline model admin — at Version 9
| Reported by: | 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 )
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 , 15 years ago
| Cc: | added |
|---|
comment:2 by , 15 years ago
| Has patch: | set |
|---|---|
| Keywords: | formset added |
| Owner: | changed from to |
| Status: | new → assigned |
| Triage Stage: | Unreviewed → Accepted |
comment:3 by , 15 years ago
| Component: | django.contrib.admin → Forms |
|---|
comment:4 by , 15 years ago
| Severity: | → Normal |
|---|---|
| Type: | → Bug |
comment:5 by , 15 years ago
| Easy pickings: | unset |
|---|---|
| Patch needs improvement: | set |
fix_inline_model_with_to_field.diff fails to apply cleanly on to trunk
by , 13 years ago
| Attachment: | to_field_formsets.patch added |
|---|
comment:7 by , 13 years ago
| Patch needs improvement: | unset |
|---|---|
| Version: | 1.2 → 1.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 , 13 years ago
| Status: | assigned → new |
|---|
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.