I have a number of models with a ForeignKey relationship to a Person
model. For example:
class PersonData(BaseModel):
person = models.ForeignKey(Person)
data = models.TextField()
I want to lock down the admin such that once a PersonData
object is created, an admin user can change the data, but can't change the Person
.
At first it seemed pretty straightforward -- I put this in the PersonDataAdmin
class:
def get_readonly_fields(self, request, obj=None):
if obj:
return self.readonly_fields + ('person',)
return self.readonly_fields
On the display side, that worked as expected -- I see the value for person
, but it's grayed out so I can't change it -- but then when I try to change the data and submit the form, I get an error message, "Please correct the error below." No other message appears, but with a bit of digging, I discovered that the form is missing a value for the required person
field.
I've investigated a solution that would involve creating a custom form that would disable this field selectively (something like this or this), but (a) I wasn't successful in getting it to work, and (b) it seemed like a ton of code for what seems like a much simpler situation. I also looked into using exclude
, but ran into the same problem as read_only
.
How can I accomplish this?