raw id fields for inherited models in inline intermediate models show base class __unicode__ rather than id
|Reported by:||Jacob Smullyan <jsmullyan@…>||Owned by:||mtredinnick|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
A raw id field targetting a model that inherits concretely from another, when appearing in an intermediate model's inline in a m2m field, will be populated upon selection, not with an id, but with a string representation of the target model's base class (which breaks). That's accurate but a bit opaque. Here is an example:
from django.contrib import admin from django.contrib.contenttypes import generic from django.db import models class Base(models.Model): title=models.CharField(max_length=80) class X(Base): number=models.IntegerField() class Y(models.Model): some_as=models.ManyToManyField(X, through='X2YRelation') class X2YRelation(models.Model): sort_number=models.IntegerField() x=models.ForeignKey(X) y=models.ForeignKey(Y) admin.site.register(X) class X2YInline(admin.TabularInline): model=X2YRelation raw_id_fields=('x',) class YAdmin(admin.ModelAdmin): inlines=[X2YInline] admin.site.register(Y, YAdmin)
If you run this and go in the admin to create a Y object, you can successfully create an X inline, but you cannot choose an existing X, because instead of an id showing up in the raw id field, you get the string "Base object", which of course makes the form blow up on submit.
The ramifications of this bug in a real life application, from which the above example is abstracted, are pretty severe, since dropdowns aren't really usable when the number of choices is unlimited -- and inline's extras cause the dropdown to be repeated, making things even worse. A workaround is to set the __unicode__ of the base class to return u"%d" % self.id , but that makes __unicode__ useless for other purposes.
(Note: I'm running the 1.0.X branch, but I've also tested the above example on trunk.)
Change History (2)
comment:1 Changed 7 years ago by mtredinnick
- Needs documentation unset
- Needs tests unset
- Owner changed from nobody to mtredinnick
- Patch needs improvement unset
- Status changed from new to assigned