Reputation: 313
I've been reading a lot but I don't seem to be able to figure out a solution to this.
I'm writing an application in Django, I'm still writing the admin side.
I have a model called "Environments" and a model called "Servers", there is a ForeignKey relation between Servers and Environments such as a given Environment has several servers.
When modifying the "add" form for Environments in the admin interface I use a Inline form to be able to visualize the list of Servers that will be associated to the Environment, something like this:
class ServerInline(admin.TabularInline):
model = Server
extra = 39
class EnvironmentAdmin(admin.ModelAdmin):
inlines = [ServerInline]
Pretty simple right?
What I would like to do is prepopulate the Servers inline forms with default values, I've been able to prepopulate them with the same value doing this:
class ServerInlineAdminForm(forms.ModelForm):
class Meta:
model = Server
def __init__(self, *args, **kwargs):
super(ServerInlineAdminForm, self).__init__(*args, **kwargs)
self.initial['name']='Testing'
class ServerInline(admin.TabularInline):
form = ServerInlineAdminForm
model = Server
extra = 39
class EnvironmentAdmin(admin.ModelAdmin):
inlines = [ServerInline]
But this isn't what I want, I would like to be able to initialize the 39 Server form instances with 39 different values that I have in a list. What would be the best way to do that??
Thank you!
Upvotes: 23
Views: 18392
Reputation: 12903
This is an old answer, a much simpler solution has become possible since, see jnns's answer with get_formset_kwargs
.
Here is my implementation, thanks to Steven for the idea.
All in admin.py:
class SecondaryModelInline(admin.ModelAdmin):
model = SecondaryModel
formset = SecondaryModelInlineFormSet
def get_formset(self, request, obj=None, **kwargs):
formset = super(SecondaryModelInline, self).get_formset(request, obj, **kwargs)
formset.request = request
return formset
def get_extra(self, request, obj=None, **kwargs):
extra = super(SecondaryModelInline, self).get_extra(request, obj, **kwargs)
something = request.GET.get('something', None)
if something:
extra = ... figure out how much initial forms there are, from the request ...
return extra
Someplace before, also in admin.py, this:
class SecondaryModelInlineFormSet(forms.models.BaseInlineFormSet):
model = SecondaryModel
def __init__(self, *args, **kwargs):
super(SecondaryModelInlineFormSet, self).__init__(*args, **kwargs)
if self.request.GET.get('something', None):
# build your list using self.request
self.initial=[{'field_a': 'A', ...}, {}... ]
Upvotes: 14
Reputation: 5644
Since Django 4.0, the ModelAdmin
has a method get_formset_kwargs
that can be used for exactly this scenario with very few lines of code:
class ServerInline(admin.TabularInline):
model = Server
class EnvironmentAdmin(admin.ModelAdmin):
inlines = [ServerInline]
def get_formset_kwargs(self, request, obj, inline, prefix):
formset_params = super().get_formset_kwargs(request, obj, inline, prefix)
if not obj.id and isinstance(inline, ServerInline):
formset_params |= {"initial": [
{"name": f"Testing {i}"} for i in range(39)
]}
return formset_params
Please note: the dict merge operator |
is only available from Python 3.9 onwards. Use dict.update()
on older versions.
Upvotes: 5
Reputation: 1229
Adding the initial
elements in the inline class as all the other answers suggest won't be enough to actually get the elements saved.
Django ignores the initial elements that were not modified by users, so you need to add some extra logic to your InlineFormSet
to save the initial data:
from django.contrib import admin
from django.forms import BaseInlineFormSet
class InitialElementsInlineFormSet(BaseInlineFormSet):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.initial_extra is not None:
self.extra = len(self.initial_extra)
def save_new_objects(self, commit=True):
super().save_new_objects(commit)
for form in self.extra_forms:
if not form.has_changed() and form.initial in self.initial_extra:
instance = form.instance
instance.__dict__.update(form.initial)
setattr(instance, self.fk.name, self.instance)
self.new_objects.append(instance)
if commit:
instance.save()
return self.new_objects
This Formset class is doing two things:
__init__
method, we're setting the initial form number to be the same as your initial elements, so all of them are displayed.initial
, it will fail to create the object, resulting in a Server Error response.Once you have created this class, you should be able to use it in your admin models like this:
class MyModelInline(admin.TabularInline):
model = MyModel
formset = InitialElementsInlineFormSet
def get_initial(self, request):
data = [
{
"field_a": "example",
"user_id": request.user.id,
"favorite_number": i,
}
for i in range(3)
]
return data
class ParentAdmin(admin.ModelAdmin):
inlines = [MyModelInline]
def get_formset_kwargs(self, request, obj, inline, prefix):
formset_params = super().get_formset_kwargs(request, obj, inline, prefix)
if isinstance(inline, MyModelInline) and obj.pk is None:
formset_params.update(initial=inline.get_initial(request))
return formset_params
Override the Inline class' get_initial
method to populate the forms you want to see when creating a new object. You can access the user object as well as data from the parent using the request
object.
In the ParentAdmin
class, we're making sure we only populate the initial keyword for our Inline
class. By checking if obj.pk is None
, we ensure this happens only during the creation of a Parent
object. You shouldn't need to modify this part.
Upvotes: 1
Reputation: 4687
It worked for me in case of prepopulating user
from request.user
for StackedInline
and TabularInline
.
def save_formset(self, request, form, formset, change):
for form in formset.forms:
form.instance.user = request.user
formset.save()
Upvotes: 1
Reputation: 807
Well, I wanted to comment on frnhr's answer, but did not have enough reputation, so:
The answer worked for me, I just needed to loop through the forms in the formset and set the initial data for each of them:
class SecondaryModelInlineFormSet(forms.models.BaseInlineFormSet):
model = SecondaryModel
def __init__(self, *args, **kwargs):
super(SecondaryModelInlineFormSet, self).__init__(*args, **kwargs)
if self.request.GET.get('something', None):
# build your list using self.request
for form in self:
form.initial = {'field_a':'A',...} #This is what I changed
self.initial=[{'field_a': 'A', ...}, {}... ]
Upvotes: 2
Reputation: 313
I realized that I solved the problem myself and hadn't answered here.
What I finally did is to override the Environment class save_model method instead for using the admin forms.
I'll explain a little bit better:
I have an environment object and a server object. An environment has a number of servers that are linked to it via a foreign key into the server object. My goal was to populate the servers associated to an environment in the environment creation process. To be able to do that what I did was override the save_model method for the Environment object, do an obj.save() and AFTERWARDS create the Server objects that point to this environment, and then obj.save() again. Why afterwards? Because I can't relation a new created server with an environment that doesn't exist yet. Let me know if there is someone interested on he actual code.
Upvotes: 4
Reputation: 2698
Not sure exactly why you want to do this, but perhaps you could create a modelformset:
from django.forms.models import BaseModelFormSet
class ServerFormSet(BaseModelFormSet):
def __init__(self, *args, **kwargs):
super(ServerFormSet, self).__init__(*args, **kwargs)
self.initial = [{ 'name': 's1', }, {'name': 's2'},] # supply your list here
and set this on your inline:
class ServerInline(admin.TabularInline):
form = ServerInlineAdminForm
model = Server
extra = 39
formset = ServerFormSet
I have not tried this out.
https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#providing-initial-values
https://docs.djangoproject.com/en/dev/topics/forms/formsets/#using-initial-data-with-a-formset
Upvotes: 8
Reputation: 1280
I haven't tried this but since the extra forms generated are essential Django Formsets, what you need to do is bind initial data to the formset which is explained in the docs here.
I just read through the docs and it looks like you can define your own formset inside your inlineadmin and then as mentioned above, prepopulate the formset with data from your list. I think you could achieve that by placing the prepopulation code in your class' init method.
I know this isn't a very elaborate explanation but I found the question interesting and looked up the docs and thought maybe I could point you in the right direction with what to try next.
Upvotes: 3