How do I filter values in a Django form using ModelForm?

You can customize your form in init class ExcludedDateForm(ModelForm): class Meta: model = models.ExcludedDate exclude = (‘user’, ‘recurring’,) def __init__(self, user=None, **kwargs): super(ExcludedDateForm, self).__init__(**kwargs) if user: self.fields[‘category’].queryset = models.Category.objects.filter(user=user) And in views, when constructing your form, besides the standard form params, you’ll specify also the current user: form = ExcludedDateForm(user=request.user)

How to set css class of a label in a django form declaration?

Widgets have an attrs keyword argument that take a dict which can define attributes for the input element that it renders. Forms also have some attributes you can define to change how Django displays your form. Take the following example: class MyForm(forms.Form): error_css_class=”error” required_css_class=”required” my_field = forms.CharField(max_length=10, widget=forms.TextInput(attrs={‘id’: ‘my_field’, ‘class’: ‘my_class’})) This works on any … Read more

Django – form.save() is not creating ModelForm

The problem was in action of form in my html page: <form action=”{% url ‘task_details’ task.slug %}” method=”POST”> {% csrf_token %} {{ form.feedback_content }} <div class=”panel-buttons”> <div class=”checkbox”> {{ form.is_solved }} </div> <div class=”save-btn-container”> <button class=”btn btn–pill btn–green” type=”submit”>Send</button> </div> </div> </form>

Customize/remove Django select box blank option

Haven’t tested this, but based on reading Django’s code here and here I believe it should work: class ThingForm(forms.ModelForm): class Meta: model = Thing def __init__(self, *args, **kwargs): super(ThingForm, self).__init__(*args, **kwargs) self.fields[‘verb’].empty_label = None EDIT: This is documented, though you wouldn’t necessarily know to look for ModelChoiceField if you’re working with an auto-generated ModelForm. EDIT: … Read more