django-forms

How to get ModelChoiceField instances in the template

北慕城南 提交于 2019-12-17 16:06:48
问题 I have a ModelForm that contains a ModelChoiceField using the RadioSelect widget. class MyAForm(forms.ModelForm): one_property = models.ModelChoiceField( widget=forms.RadioSelect, queryset=MyBModel.objects.filter(visible=True), empty_label=None) class Meta: model = MyAModel There are attributes on MyBModel that I want to display next to the radio button. I would override label_from_instance on a sub-class of ModelChoiceField but this does not allow me to do what I want as I want the radio

Using AuthenticationForm in Django

前提是你 提交于 2019-12-17 15:47:16
问题 I'm attempting to use the AuthenticationForm form with django and finding out that I can't seem to get the form to validate. I widdled it down to a simple test(assuming it's correct) that doesn't seem to work. Can anyone see a problem here? >>> from django.contrib.auth.forms import AuthenticationForm >>> POST = { 'username': 'test', 'password': 'me', } >>> form = AuthenticationForm(POST) >>> form.is_valid() False Is there a real reason it won't validate? Am I using it incorrectly? I've

How to limit file types on file uploads for ModelForms with FileFields?

时光总嘲笑我的痴心妄想 提交于 2019-12-17 15:43:26
问题 My goal is to limit a FileField on a Django ModelForm to PDFs and Word Documents. The answers I have googled all deal with creating a separate file handler, but I am not sure how to do so in the context of a ModelForm. Is there a setting in settings.py I may use to limit upload file types? 回答1: I handle this by using a clean_[your_field] method on a ModelForm. You could set a list of acceptable file extensions in settings.py to check against in your clean method, but there's nothing built

Overriding initial value in ModelForm

只谈情不闲聊 提交于 2019-12-17 15:36:01
问题 in my Django (1.2) project, I want to prepopulate a field in a modelform, but my new value is ignored. This is the snippet: class ArtefactForm(ModelForm): material = CharField(widget=AutoCompleteWidget('material', force_selection=False)) def __init__(self, *args, **kwargs): super(ArtefactForm, self).__init__(*args, **kwargs) self.fields['material'].initial = 'Test' I also tried with self.base_fields , but no effect: there is always the database-value displaying in the form. Any ideas? 回答1:

Django: How to override form.save()?

自古美人都是妖i 提交于 2019-12-17 15:32:09
问题 My model has quite a few boolean fields. I've broken these up into 3 sets which I'm rendering as a MultipleChoiceField w/ a modified CheckboxSelectMultiple . Now I need to save this data back to the DB. i.e., I need to split the data returned by a single widget into multiple boolean columns. I think this is appropriate for the save() method, no? Question is, how do I do I do it? Something like this? def save(self, commit=True): # code here return super(MyForm, self).save(commit) If so... how

Django ModelForm: What is save(commit=False) used for?

自作多情 提交于 2019-12-17 15:26:29
问题 Why would I ever use save(commit=False) instead of just creating a form object from the ModelForm subclass and running is_valid() to validate both the form and model? In other words, what is save(commit=False) for? If you don't mind, could you guys provide hypothetical situations where this might be useful? 回答1: That's useful when you get most of your model data from a form, but need to populate some null=False fields with non-form data. Saving with commit=False gets you a model object, then

Django: writing a view to delete an item with checkboxes

这一生的挚爱 提交于 2019-12-17 10:56:38
问题 I need help writing a view that gets the POST data which then finds out which checkboxes have been checked, and then deletes the items from the database matched by id. Originally, This was a checkbox problem. In a edit order form, there are a list of items. Now I CAN remove the item using a bit of javascript but it does not get save because I need to remove it completly from my database. I tried using my edit order views to remove an item using the delete(), but that doe not work. There must

Get type of Django form widget from within template

落花浮王杯 提交于 2019-12-17 08:32:08
问题 I'm iterating through the fields of a form and for certain fields I want a slightly different layout, requiring altered HTML. To do this accurately, I just need to know the widget type. Its class name or something similar. In standard python, this is easy! field.field.widget.__class__.__name__ Unfortunately, you're not allowed access to underscore variables in templates. Great! You can test field.field.widget.input_type but this only works for text/password <input ../> types. I need more

Django multi-select widget?

孤街醉人 提交于 2019-12-17 07:03:06
问题 The Django admin site makes use of a really cool widget: How can I make use of this widget in my own applications? I don't see anything like that listed here. 回答1: 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

Inline Form Validation in Django

家住魔仙堡 提交于 2019-12-17 06:24:09
问题 I would like to make an entire inline formset within an admin change form compulsory. So in my current scenario when I hit save on an Invoice form (in Admin) the inline Order form is blank. I'd like to stop people creating invoices with no orders associated. Anyone know an easy way to do that? Normal validation like ( required=True ) on the model field doesn't appear to work in this instance. 回答1: The best way to do this is to define a custom formset, with a clean method that validates that