I have form like this:
class TitlePropose(forms.Form):
title = forms.CharField(max_length=128)
code= forms.CharField(max_length=32)
def __init__(self, contest, *args, **kwargs):
super(TitlePropose, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = self.__class__.__name__.lower()
self.helper.form_action = ''
self.helper.layout = Layout(,
Field('title'),
Field('code'),
)
def clean_title(self):
if OtherModel.objects.filter(contest=contest, title=self.cleaned_data['title']).count() > 0:
raise forms.ValidationError("Title unavailable")
else:
return self.cleaned_data['title']
I try to access variable "contest" from clean_title method, without any success. I'm passing this variable in form class contructor:
#contest is just some object
new_title_form = TitlePropose(contest=contest.uuid)
Any suggestion, how can I get access 'contest' in clean_title?
This is standard Python class stuff. If you want to store an object so that other methods can access it, you make it an instance attribute by adding it to self.
def __init__(self, *args, **kwargs):
self.contest = kwargs.pop('contest')
super(TitlePropose, self).__init__(*args, **kwargs)
def clean_title(self):
if OtherModel.objects.filter(contest=self.contest, ...
Related
I have a form for which I'd like to use user data to filter the content of a choicefield.
Following this solution, I added all references to user in the __init__ function of my Form class:
class MyChoiceField(forms.Form):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super(MyChoiceField, self).__init__(*args, **kwargs)
user_id = self.user.id
myobjects = forms.ModelChoiceField(label='',queryset = Myobject.objects.values_list('name', flat=True).exclude(name__isnull=True).filter(Q(person__isnull=True) | Q(person=user_id)).distinct(),empty_label=None)
And in the view I call it as:
def my_view(request):
my_list = MyChoiceField(user = request.user)
context = {
'my_list': my_list,
}
return render(request, 'foo/bar.html', context)
Debugging the __init__ part indicates the queryset content is correct, but in the view, my_list contains the following:<MyChoiceField bound=False, valid=Unknown, fields=()>.
Should I include something in the form class, outside of the __init__ part for this to work?
try this
class MyChoiceField(forms.Form):
def __init__(self, *args, **kwargs):
user = kwargs.pop('user')
super(MyChoiceField, self).__init__(*args, **kwargs)
user_id = user.id
I've got a first form as following:
class SupplierRegistrationSupplementForm(forms.ModelForm):
siret = FRSIRETField()
def __init__(self, *args, **kwargs):
super(SupplierRegistrationSupplementForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = 'form-horizontal'
self.helper.form_id = 'company_supplement_form'
self.helper.form_action = "."
self.helper.label_class = 'col-lg-2'
self.helper.field_class = 'col-lg-8'
self.helper.add_input(
Submit('submit', _('Save'),
css_class="btn-primary btn-lg",
css_id="save_company"
)
)
I need to inherit from it but with a different button. In order to change the button id, I would have done this:
class SupplierUpdateForm(SupplierRegistrationSupplementForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper.form_tag = False
self.helper.add_input(
Submit('submit', _('Save'),
css_class="btn-primary btn-lg",
css_id="save_user"
)
)
But it adds a new button to the previous one. Is it possible to remove the first one?
When you call add_inputs, the code appends the input to self.inputs. So a quick hack would be to pop the existing input from the list before adding the new one.
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
...
self.inputs.pop()
self.helper.add_input(...)
However this is fragile, because it assumes that there is exactly one input. It might be better to have a BaseSupplierForm which does not have any inputs, then have two subclasses SupplierRegistrationSupplementForm and SupplierUpdateForm, which both define their own inputs.
I am trying to leave my object itself out of the queryset of possible options. Problem is i get the error: 'Country' object is not iterable
Not sure where i am going wrong.
My view:
def edit_country(request, country_id):
country = get_object_or_404(Country, pk=country_id)
country_form = CountryForm(instance=country)
return render(request, 'create_country.html', {'country_form': country_form})
My form init:
def __init__(self, *args, **kwargs):
super(CountryForm, self).__init__(*args, **kwargs)
if 'instance' in kwargs:
self.fields['likes'].queryset = Country.objects.exclude(kwargs['instance'])
self.fields['hates'].queryset = Country.objects.exclude(kwargs['instance'])
Where do i go wrong?
Change the order of the method, so you pop the kwarg first. You are sending the kwarg to super.
def __init__(self, *args, **kwargs):
instance = kwargs.pop('instance', None)
#all other stuff
I'm trying to get a placeholder to show up for a form field but I'm doing something wrong with my form fields. Any thoughts? I've got the following classes:
class EditField(forms.CharField):
def __init__(self, *args, **kwargs):
super(EditField, self).__init__(*args, **kwargs)
self.widget = forms.Textarea(attrs={'id':'editor'})
self.label = _('content')
class EditField_typeA(EditField):
def __init__(self, *args, **kwargs):
super(EditField_typeA, self).__init__(*args, **kwargs)
def clean(self, value):
if not (len(re.sub('[ ]{2,}', ' ', value)) < settings.FORM_MIN):
raise forms.ValidationError(_('content must be %s') % settings.FORM_MIN)
return value
class FinalForm(forms.Form):
foo = FooField()
bar = BarField()
text = EditField_typeA()
def __init__(self, data=None, user=None, *args, **kwargs):
super(FinalForm, self).__init__(data, *args, **kwargs)
## THIS IS THE PART THAT ISN'T WORKING
self.fields['text'].widget.attrs['placeholder'] = 'Fill this in'
if int(user.reputation) < settings.CAPTCHA_IF_REP_LESS_THAN and not (user.is_superuser or user.is_staff):
spam_fields = call_all_handlers('create_anti_spam_field')
if spam_fields:
spam_fields = dict(spam_fields)
for name, field in spam_fields.items():
self.fields[name] = field
self._anti_spam_fields = spam_fields.keys()
else:
self._anti_spam_fields = []
I'm guessing that I'm using widget wrong, or in the wrong place. Can't find the right part of the widget docs to explain what I'm doing wrong though.
I have a custom form to which I would like to pass a parameter.
Following this example I came up with the following code :
class EpisodeCreateForm(forms.Form):
def __init__(self, *args, **kwargs):
my_arg = kwargs.pop('my_arg')
super(EpisodeCreateForm, self).__init__(*args, **kwargs)
my_field = forms.CharField(initial=my_arg)
But I get the following error:
Exception Value: name 'my_arg' is not defined
How can I get it to recognize the argument in the code of the form ?
You need to set the initial value by referring to the form field instance in __init__. To get access to the form field instance in __init__, put this before the call to super:
self.fields['my_field'].initial=my_arg
And remove initial=my_arg from where you declare my_field because at that point (when class is declared) my_arg is not in scope.
The thing is that my_field is initialized when the class is created, but my_arg is initialized when a new instance is created, far too late for my_field to know its value. What you can do is initialize my_field in __init__ too:
def __init__(self, *args, **kwargs):
my_arg = kwargs.pop('my_arg')
super(EpisodeCreateForm, self).__init__(*args, **kwargs)
if not self.my_field:
self.my_field = my_arg
This code is executed once at import time:
my_field = forms.CharField(initial=my_arg)
and this code is executed on form instance creation:
my_arg = kwargs.pop('my_arg')
super(EpisodeCreateForm, self).__init__(*args, **kwargs)
So this won't work this way. You should set initial value for the field in your __init__ method.
By the way, all this seems unnecessary, why don't use 'initial' keyword in a view?
Considering your comment, I would do this:
class EpisodeCreateForm(forms.Form):
def __init__(self, *args, **kwargs):
self.my_arg = kwargs.pop('my_arg')
kwargs.setdefault('initial', {})['my_field'] = self.my_arg
super(EpisodeCreateForm, self).__init__(*args, **kwargs)
def save(self):
do_something(self.my_arg)
...
super(EpisodeCreateForm, self).save()
my_field = forms.CharField()
Passing initial to the superclass and letting it do the work seems cleaner to me than directly setting it on the field instance.
You simply need to pop your arg before super() and put it in the fields dictionnary after super() :
class EpisodeCreateForm(forms.Form):
my_field = forms.CharField(label='My field:')
def __init__(self, *args, **kwargs):
my_arg = kwargs.pop('my_arg')
super(EpisodeCreateForm, self).__init__(*args, **kwargs)
self.fields['my_arg'].initial = my_arg
Then, simply call
form = EpisodeCreateForm (my_arg=foo)
As an example, say you have a table of Episodes, and you want to show the availables ones in a choices menu, and select the current episode. For that, use a ModelChoiceField:
class EpisodeCreateForm(forms.Form):
available_episode_list = Episode.objects.filter(available=True)
my_field = forms.ModelChoiceField(label='My field:',
queryset=available_episode_list)
def __init__(self, *args, **kwargs):
cur_ep = kwargs.pop('current_episode')
super(EpisodeCreateForm, self).__init__(*args, **kwargs)
self.fields['current_episode'].initial = cur_ep