Grouped CheckboxSelectMultiple in Django template

You have to write the custom CheckboxSelectMultiple widget. Using the snippet I have tried make the CheckboxSelectMultiple field iterable by adding the category_name as an attribute in field attrs. So that I can use regroup tag in template later on. The below code is modified from snippet according to your need, obviously this code can … Read more

Django multi-select widget?

From the docs: The Django Admin application defines a number of customized widgets for calendars, filtered selections, and so on. These widgets define media requirements, and the Django Admin uses the custom widgets in place of the Django defaults. The Admin templates will only include those media files that are required to render the widgets … Read more

Dropdown in Django Model

From model to template : models.py COLOR_CHOICES = ( (‘green’,’GREEN’), (‘blue’, ‘BLUE’), (‘red’,’RED’), (‘orange’,’ORANGE’), (‘black’,’BLACK’), ) class MyModel(models.Model): color = models.CharField(max_length=6, choices=COLOR_CHOICES, default=”green”) forms.py class MyModelForm(ModelForm): class Meta: model = MyModel fields = [‘color’] views.py class CreateMyModelView(CreateView): model = MyModel form_class = MyModelForm template_name=”myapp/template.html” success_url=”myapp/success.html” template.html <form action=”” method=”post”>{% csrf_token %} {{ form.as_p }} <input … Read more

Initial populating on Django Forms

S. Lott’s answer tells you how to initialize the form with some data in your view. To render your form in a template, see the following section of the django docs which contain a number of examples: Outputting forms as HTML Although the examples show the rendering working from a python interpreter, it’s the same … Read more

Django/jQuery Cascading Select Boxes?

Here is my solution. It uses the undocumented Form method _raw_value() to peek into the data of the request. This works for forms, which have a prefix, too. class CascadeForm(forms.Form): parent=forms.ModelChoiceField(Parent.objects.all()) child=forms.ModelChoiceField(Child.objects.none()) def __init__(self, *args, **kwargs): forms.Form.__init__(self, *args, **kwargs) parents=Parent.objects.all() if len(parents)==1: self.fields[‘parent’].initial=parents[0].pk parent_id=self.fields[‘parent’].initial or self.initial.get(‘parent’) \ or self._raw_value(‘parent’) if parent_id: # parent is known. … Read more

Django JQuery Ajax File Upload

Here is what I changed to get it working. I used FormData to package up data from form Notice the parameters of the form in the Django view. I was not specifying “files” before and that’s what caused the ” file field required” error. Javascript: function upload(event) { event.preventDefault(); var data = new FormData($(‘form’).get(0)); $.ajax({ … Read more

How to change the file name of an uploaded file in Django?

How are you uploading the file? I assume with the FileField. The documentation for FileField.upload_to says that the upload_to field, may also be a callable, such as a function, which will be called to obtain the upload path, including the filename. This callable must be able to accept two arguments, and return a Unix-style path … Read more

Inline Form Validation in Django

The best way to do this is to define a custom formset, with a clean method that validates that at least one invoice order exists. class InvoiceOrderInlineFormset(forms.models.BaseInlineFormSet): def clean(self): # get forms that actually have valid data count = 0 for form in self.forms: try: if form.cleaned_data: count += 1 except AttributeError: # annoyingly, if … Read more

Django: How to get current user in admin forms?

Here is what i did recently for a Blog: class BlogPostAdmin(admin.ModelAdmin): form = BlogPostForm def get_form(self, request, **kwargs): form = super(BlogPostAdmin, self).get_form(request, **kwargs) form.current_user = request.user return form I can now access the current user in my forms.ModelForm by accessing self.current_user EDIT: This is an old answer, and looking at it recently I realized the … Read more