pre-populate tabular inline in admin

195 views
Skip to first unread message

carlos

unread,
Oct 28, 2014, 1:47:41 AM10/28/14
to django...@googlegroups.com
Hi, i need pre-populete tabular inline in admin what is the best ways
example

class Options(models.Model):
     name = CharField(...)


class ModelA(models.Model):
    option = FK(Options)
    field_1 = IntegerField()
    field_2 = IntegerField()
    field_3 = CharField()
    field_4 = booleanField()

in forms.py
class ModelAForm(ModelForm):
    class Meta:
         model = ModelA

ModelAFormSet = formsets.formset_factory(ModelAForm, extra=Option.objects.count())

#but I do not understand when to use initial

in admin.py
extra_count = Options.objects.all().count()

class ModelAInline(admin.TabularInline)
    model = ModelA
    formset = ModelAFormSet
    extra = extra_count
    max_num = extra_count


but i don't understand complete, i need my admin equal this
i have 4 Option in my class Options

option      field_1     field_2     field_3     field_4
--------------------------------------------------------------
option1      __           __            __           ___
option2      __           __            __           ___
option3      __           __            __           ___
option4      __           __            __           ___

i don't need add another !

I need all deployed only introduce additional data field_x (1,2,3,4)

as I can do this in the admin ??

any link? example? idea?

Cheers




Collin Anderson

unread,
Oct 29, 2014, 2:19:29 PM10/29/14
to django...@googlegroups.com
Hello,

This one's tricky. It may be simplest to just _create_ the empty inline ModelA's so they appear. Otherwise, you'll need to look at the ModelA's that exist in the database and figure out which ones needed to be added to the initial data.

You'll likely need to add the initial data using the get_formsets_with_inlines hook.

And you can always peak at the source code to get a better feel for what's going on :)

Collin

Reply all
Reply to author
Forward
0 new messages