Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#26060 closed Bug (fixed)

Regression in reverse one-to-one field when in readonly_fields

Reported by: Steven Davidson Owned by: Sasha Gaevsky
Component: contrib.admin Version: 1.8
Severity: Release blocker Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Steven Davidson)

When upgrading from 1.7 to 1.8(.8) I started encountering errors in the admin with a reverse one to one relationship when it was in readonly_fields. I assume this is a regression. Naturally, I am happy to help fix it (if it is not by design), given a steer!

The attached simple project shows the issue (it was created with Django 1.8.8). Python 2.7.10

cd /tmp
git clone https://github.com/damycra/django-readonly-onetoone.git
cd django-readonly-onetoone/
mkvirtualenv dj_onetoone

# make sure virtualenv is activated, then:
pip install -r requirements.txt
./manage.py migrate
./manage.py createsuperuser
./manage.py runserver
# open http://127.0.0.1:8000/admin/simple/a/add/
# create an A object
# create a B object referencing the A http://127.0.0.1:8000/admin/simple/b/add
# return to original A object http://127.0.0.1:8000/admin/simple/a/1/

# now upgrade to Django 1.8.8
pip install Django==1.8.8
./manage.py migrate
./manage.py runserver
# return to http://127.0.0.1:8000/admin/simple/a/1/
AttributeError at /admin/simple/a/1/
OneToOneRel object has no attribute 'rel
'

# now upgrade to Django 1.9.1
pip install Django==1.9.1
./manage.py migrate
./manage.py runserver
# go to http://127.0.0.1:8000/admin/simple/a/1/change/
AttributeError at /admin/simple/a/1/change/
'OneToOneRel' object has no attribute 'flatchoices
'

A simple workaround is to use a method on ModelAdmin

class WorkingAAdmin(admin.ModelAdmin):
    readonly_fields = ['working_b']
    def working_b(self, instance):
        return instance.b
    working_b.short_description = 'working'

See also https://code.djangoproject.com/ticket/24851#comment:8

Attachments (1)

django-readonly-onetoone-master.zip (6.4 KB) - added by Steven Davidson 4 years ago.
Zip of Github project

Download all attachments as: .zip

Change History (9)

Changed 4 years ago by Steven Davidson

Zip of Github project

comment:1 Changed 4 years ago by Steven Davidson

Description: modified (diff)

comment:2 Changed 4 years ago by Tim Graham

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

comment:3 Changed 4 years ago by Sasha Gaevsky

Owner: changed from nobody to Sasha Gaevsky
Status: newassigned

comment:4 Changed 4 years ago by Sasha Gaevsky

Has patch: set

comment:5 Changed 4 years ago by Tim Graham

Triage Stage: AcceptedReady for checkin

comment:6 Changed 4 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 9a33d3d:

Fixed #26060 -- Fixed crash with reverse OneToOneField in ModelAdmin.readonly_fields.

comment:7 Changed 4 years ago by Tim Graham <timograham@…>

In be3169d6:

[1.9.x] Fixed #26060 -- Fixed crash with reverse OneToOneField in ModelAdmin.readonly_fields.

Backport of 9a33d3d76497d9e198de942ee1236c452231262f from master

comment:8 Changed 4 years ago by Tim Graham <timograham@…>

In 8502e9f0:

[1.8.x] Fixed #26060 -- Fixed crash with reverse OneToOneField in ModelAdmin.readonly_fields.

Backport of 9a33d3d76497d9e198de942ee1236c452231262f from master

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