Answer the question
In order to leave comments, you need to log in
How to get custom field attributes in a template?
I made my own class for the form field with additional attributes:
class ModelChoiceFieldWithLinks(forms.ModelChoiceField):
def __init__(self, *args, **kwargs):
self.add_url = kwargs.pop('add_url')
self.edit_url = kwargs.pop('edit_url')
super(ModelChoiceFieldWithLinks, self).__init__(*args, **kwargs)
factory = ModelChoiceFieldWithLinks(queryset=Factory.objects.all(),
add_url=reverse_lazy('factory_add'), edit_url=reverse_lazy('factory_edit'),
required=False,
widget=forms.Select(attrs={'class': 'form-control'}))
field.add_url
and field.field.add_url
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question