File tree Expand file tree Collapse file tree 3 files changed +30
-4
lines changed Expand file tree Collapse file tree 3 files changed +30
-4
lines changed Original file line number Diff line number Diff line change @@ -414,6 +414,20 @@ def get_ordering(self, request):
414
414
"""
415
415
return self .ordering or () # otherwise we might try to *None, which is bad ;)
416
416
417
+ def _get_readonly_manual_pk_fields (self , obj ):
418
+ """
419
+ Returns a list containing the primary key field name if:
420
+ - we're editing an existing object,
421
+ - the PK was manually defined (not auto-created),
422
+ - and it's not already in self.readonly_fields.
423
+ """
424
+ if not obj :
425
+ return []
426
+ pk = self .model ._meta .pk
427
+ if pk .editable and not pk .auto_created and pk .name not in self .readonly_fields :
428
+ return [pk .name ]
429
+ return []
430
+
417
431
def get_readonly_fields (self , request , obj = None ):
418
432
"""
419
433
Hook for specifying custom readonly fields.
Original file line number Diff line number Diff line change @@ -109,10 +109,6 @@ body {
109
109
background : var (--body-bg );
110
110
}
111
111
112
- label {
113
- font-size : 1rem ;
114
- }
115
-
116
112
/* LINKS */
117
113
118
114
a : link , a : visited {
Original file line number Diff line number Diff line change 19
19
Child ,
20
20
ChildModel1 ,
21
21
ChildModel2 ,
22
+ EditablePKBook ,
22
23
ExtraTerrestrial ,
23
24
Fashionista ,
24
25
FootNote ,
@@ -708,6 +709,21 @@ def test_inline_editable_pk(self):
708
709
count = 1 ,
709
710
)
710
711
712
+ def test_inline_manual_pk_is_readonly_when_editing (self ):
713
+ author = Author .objects .create (name = "Jane Austen" )
714
+ EditablePKBook .objects .create (
715
+ author = author , manual_pk = 101 , title = "Pride and Prejudice"
716
+ )
717
+
718
+ response = self .client .get (
719
+ reverse ("admin:admin_inlines_author_change" , args = [author .pk ])
720
+ )
721
+
722
+ self .assertContains (
723
+ response , 'name="editablepkbook_set-0-manual_pk"' , html = False
724
+ )
725
+ self .assertContains (response , "readonly" , html = False )
726
+
711
727
def test_stacked_inline_edit_form_contains_has_original_class (self ):
712
728
holder = Holder .objects .create (dummy = 1 )
713
729
holder .inner_set .create (dummy = 1 )
You can’t perform that action at this time.
0 commit comments