django-nested-admin nested model initial values

20 views Asked by At

I've been working with django-nested-admin for editing admin models that have foreign key relationships to other tables. Frankly it's beautiful. I love it.

But I have a pair of models here I'm having a bit of trouble with.

I have a Chapter, representing a chapter of a club. Each chapter has a one or more useful links to things like their web page or wiki articles or whatever.

Both classes have a field of 'registrar' which is the user that created them. Basically whoever was logged in to the system when the instance was created.

For Chapter, this went fairly easily. I just added

def get_changeform_initial_data(self, request):
    get_data = super(ChapterAdmin, self).get_changeform_initial_data(request)
    get_data['registrar'] = request.user.pk
    return get_data

To the ChapterAdmin class. It pre-populates just fine.

But when I tried to do the same with the UsefulLinksInline, that ChapterAdmin uses. I've been down a rabbit hole of googling and break-point surfing trying to figure out how to pre-populate the UsefulLink class's registrar field with the current user.

In UsefulLinksInline, if I remove the registrar field from the fields=() list, I get an error stating that the null constraint on registrar was being violeted.

If I leave it in, however, the Registrar combo list appears in the UsefulLinksInline. I can pick one sure, and it will work. But I don't see any reason to pick one. I just want to use the current user because that's the one that's creating it.

I tried overriding some of the base functions of UsefulLinksInline and it's base classes to see if I can pre-populate that field. But either the breakpoint never fires or it just doens't do anything or it throws an error. I even tried overriding init to no avail.

Does anyone have any idea how to deal with this?

enter image description here

0

There are 0 answers