23

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!

user1664820
  • 303
  • 1
  • 3
  • 11

6 Answers6

11

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', ...}, {}... ]
Community
  • 1
  • 1
frnhr
  • 11,308
  • 9
  • 55
  • 83
8

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.

See: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin.formset

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

Steven
  • 2,528
  • 12
  • 15
  • This work fine after made some fixes to the code: **1.** Use `BaseInlineFormSet` instead `BaseModelFormSet` and **2.** set `initial` before call `__init__`: ,`kwargs.update({'initial': [{ 'name': 's1', }, {'name': 's2'},]})`, set `self.initial` after super call `__init__` not works for me. – juliocesar Aug 16 '16 at 14:54
4

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.

user1664820
  • 303
  • 1
  • 3
  • 11
3

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.

Community
  • 1
  • 1
keithxm23
  • 1,270
  • 1
  • 21
  • 41
2

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', ...}, {}... ]
pedrovgp
  • 655
  • 8
  • 22
1

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()
Artem Bernatskyi
  • 3,350
  • 2
  • 23
  • 29