On Tue, Mar 5, 2013 at 4:43 PM, Marc Aymerich <[email protected]> wrote:
> > > On Tue, Mar 5, 2013 at 3:11 AM, Russell Keith-Magee < > [email protected]> wrote: > >> >> >> On Mon, Mar 4, 2013 at 10:31 PM, Marc Aymerich <[email protected]>wrote: >> >>> Hi, >>> I've spend several hours trying to figure out how to save inlines with >>> initial data in them. >>> >>> basically I have a very simple model with 2 fields: >>> >>> class DirectIface(models.Model): >>> parent = models.ForeignKey('nodes.Node') >>> name = models.CharField(max_lenght=64) >>> >>> And what I'm doing is defining a formset with initial data in it: >>> >>> class DirectIfaceInlineFormSet(BaseInlineFormSet): >>> def __init__(self, *args, **kwargs): >>> kwargs['initial'] = [{ 'name': 'eth1', }, {'name': 'eth2'},] >>> super(DirectIfaceInlineFormSet, self).__init__(*args, **kwargs) >>> >>> >>> In this case 2 Direct ifaces with names eth1 and eth2. >>> >>> The problem is that those ifaces doesn't get stored when I hit the save >>> button, that's because has_changed == False. >>> So is there a way to tell Django to save formsets with only initial data >>> ? >>> >> >> Sure - just provide the initial data as actual data. If, instead of using >> 'initial', you pass the same content as 'data', the form will be saveable. >> > > Jops, just tried > > > class DirectIfaceInlineFormSet(BaseInlineFormSet): > def __init__(self, *args, **kwargs): > kwargs['data'] = [{ 'name': 'eth1', }, {'name': 'eth2'},] > super(DirectIfaceInlineFormSet, self).__init__(*args, **kwargs) > > but I'm getting an AttributeError "'list' object has no attribute 'get'" > when i'm rendering the form. > yeah, finally I did something like class DirectIfaceInlineFormSet(BaseInlineFormSet): def __init__(self, *args, **kwargs): if not kwargs['instance'].pk: kwargs['data'] = { 'direct_ifaces-TOTAL_FORMS': u'3', 'direct_ifaces-INITIAL_FORMS': u'0', 'direct_ifaces-MAX_NUM_FORMS': u'', 'direct_ifaces-0-name': u'eth0', 'direct_ifaces-1-name': u'eth1', 'direct_ifaces-2-name': u'eth2',} else: self.extra = 1 super(DirectIfaceInlineFormSet, self).__init__(*args, **kwargs) that it works :) -- Marc -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.

