I noticed that when you try to save an instance of an object with PK an ObjectId that it is transformed into a string and consequently no longer corresponds to an instance of an object, so with the override of the get_form method in POST you can intercept this data and change the string to ObjectId, but as you can see in the [Django documentation][1]:
The QueryDicts at request.POST and request.GET will be immutable when
accessed in a normal request/response cycle.
so you can use the recommendation from the same documentation:
To get a mutable version you need to use QueryDict.copy()
or ... use a little trick, for example, if you need to keep a reference to an object for some reason or leave the object the same:
# remember old state
_mutable = data._mutable
# set to mutable
data._mutable = True
# сhange the values you want
data['param_name'] = 'new value'
# set mutable flag back
data._mutable = _mutable
where data it is your QueryDicts
In this case:
@admin.register(MyMode)
class MyModelAdmin(admin.ModelAdmin):
list_display = ('....')
....
def get_form(self, request, obj, change, **kwargs):
if request.POST:
# remember old state
_mutable = request.POST._mutable
# set to mutable
request.POST._mutable = True
# сhange the values you want
request.POST['user'] = ObjectId(request.POST['user'])
request.POST['post'] = ObjectId(request.POST['post'])
request.POST['group'] = ObjectId(request.POST['group'])
# set mutable flag back
request.POST._mutable = _mutable
return super().get_form(request, obj=obj, change=change, **kwargs)