Skip to content

Instantly share code, notes, and snippets.

@chapkovski
Created September 18, 2017 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chapkovski/b6fd6134124d88d671ed7b3b1baa4b81 to your computer and use it in GitHub Desktop.
Save chapkovski/b6fd6134124d88d671ed7b3b1baa4b81 to your computer and use it in GitHub Desktop.
solution to "how to pass the parameter to a form during formset creation"
import floppyforms.__future__ as forms
from .models import SendReceive, Player
from django.forms import inlineformset_factory, BaseFormSet, BaseInlineFormSet
class SRForm(forms.ModelForm):
class Meta:
model = SendReceive
fields = ['amount_sent']
def __init__(self, *args, **kwargs):
super(SRForm, self).__init__(*args, **kwargs)
curmax = int(kwargs['instance'].sender.participant.vars['herd_size'])
self.fields['amount_sent'] = forms.IntegerField(label='How much to sent?',
required=True,
max_value=curmax,
min_value=0,
)
SRFormSet = inlineformset_factory(Player, SendReceive,
fk_name='sender',
can_delete=False,
extra=0,
form=SRForm,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment