Opened 5 years ago
Closed 5 years ago
#31449 closed Bug (duplicate)
Autocomplete doesn't work when ForeignKey to_field is not pk.
Reported by: | 007 | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
if a ForeignKey field with to_field (not pk), and the field set to autocomplete_fields.
form will get pk not to_filed_value when edit model in change_view.
example:
ObjectC field a in AutocompleteSelect will got pk not code. save fail
models:
class ObjectA(models.Model): code = models.CharField(verbose_name='code', max_length=32, unique=True) name = models.CharField(verbose_name='name', max_length=32) def __str__(self): return self.name class ObjectB(models.Model): name = models.CharField(verbose_name='name', max_length=32) a = models.ForeignKey(ObjectA, on_delete=models.CASCADE, related_name="+") ma = models.ManyToManyField(ObjectA) def __str__(self): return self.name class ObjectC(models.Model): name = models.CharField(verbose_name='name', max_length=32) a = models.ForeignKey(ObjectA, on_delete=models.CASCADE, to_field='code', related_name="+") ma = models.ManyToManyField(ObjectA, through='ObjectCtoA') def __str__(self): return self.name class ObjectCtoA(models.Model): c = models.ForeignKey(ObjectC, on_delete=models.CASCADE, related_name="+") a = models.ForeignKey(ObjectA, on_delete=models.CASCADE, to_field='code', related_name="+") class Meta: auto_created = True
admin:
class AdminA(admin.ModelAdmin): list_display = ('id', 'code', 'name') search_fields = ('code', 'name') class AdminB(admin.ModelAdmin): list_display = ('id', 'name', 'a') autocomplete_fields = ('a', 'ma') class AdminC(admin.ModelAdmin): list_display = ('id', 'name', 'a') autocomplete_fields = ('a', 'ma') site.register(models.ObjectA, AdminA) site.register(models.ObjectB, AdminB) site.register(models.ObjectC, AdminC)
Change History (1)
comment:1 by , 5 years ago
Resolution: | → duplicate |
---|---|
Status: | assigned → closed |
Summary: | Autocomplete dosnot work when ForeignKey to_field is not pk → Autocomplete doesn't work when ForeignKey to_field is not pk. |
Note:
See TracTickets
for help on using tickets.
Duplicate of #29138.