﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29981	"""Please correct the error below."" (with no errors displayed) when changing an inline that has a one-to-one relation to the parent that uses to_field"	Bernie	Patrik Sletmo	"

**Code sample:**

models.py:

{{{
from django.db import models

class Entry(models.Model):
    slug = models.SlugField(max_length=80, unique=True)
    title = models.CharField(max_length=255, blank=True, null=True)

class EntryDetail(models.Model):
    entry = models.OneToOneField(
        Entry,
        to_field='slug',
        primary_key=True,
        on_delete=models.CASCADE
    )
    description = models.TextField(blank=True, null=True)
}}}

admin.py

{{{
from django.contrib import admin
from .models import Entry, EntryDetail

class EntryDetailInline(admin.StackedInline):
    model = EntryDetail

@admin.register(Entry)
class EntryAdmin(admin.ModelAdmin):
    inlines = [EntryDetailInline]

    def get_readonly_fields(self, request, obj=None):
        # Even with EntryDetail.entry_id ON UPDATE: CASCADE set on DB-level
        # changing Entry.slug through the admin-change-form with EntryDetail-inline fails.
        # Set slug readonly as workaround. Don't know if this is worth fixing.
        readonly_fields = super().get_readonly_fields(request, obj)
        if hasattr(obj, 'entrydetail') and 'slug' not in readonly_fields:
            readonly_fields += ('slug',)
        return readonly_fields
}}}

**Steps to reproduce the bug:**

In the admin:
* add an Entry
* fill slug, title, and ""Entry detail"" description
* save and continue editing
* change ""Entry detail"" description
* save
* Error message in admin: ""Please correct the error below."" (with no errors listed). `formset[0].errors` is `[{'entry': ['The inline value did not match the parent instance.']}]` which comes from `InlineForeignKeyField` value is `aaa` and `orig` is `1`.

**Workaround:**

in models.py replace

{{{
class EntryDetailInline(admin.StackedInline):
    model = EntryDetail
}}}

with:

{{{
from django.forms import BaseInlineFormSet

class EntryDetailFormSet(BaseInlineFormSet):
    def add_fields(self, form, index):
        super().add_fields(form, index)
        related_name = self._pk_field.remote_field.related_name or self._pk_field.remote_field.name
        if hasattr(self.instance, related_name):
            form.fields[self._pk_field.name].to_field = self._pk_field.remote_field.field_name

class EntryDetailInline(admin.StackedInline):
    model = EntryDetail
    formset = EntryDetailFormSet
}}}"	Bug	closed	contrib.admin	2.1	Normal	fixed		Sergey Fedoseev	Accepted	1	0	0	0	0	0
