django-forms

Django form not saving with ModelChoiceField - ForeignKey

 ̄綄美尐妖づ 提交于 2019-12-23 19:38:34
问题 I have multiple forms on my site that work and save info to my PostgreSQL database. I am trying to create a form to save information for my Set Model: class Set(models.Model): settitle = models.CharField("Title", max_length=50) setdescrip = models.CharField("Description", max_length=50) action = models.ForeignKey(Action) actorder = models.IntegerField("Order number") The Set Form looks like this. I am using ModelChoiceField to pull a list of Action name fields from the Action model, this

How do you add a non-editable field to a custom admin form in Django

ⅰ亾dé卋堺 提交于 2019-12-23 18:09:48
问题 I am trying to add an editable=False field to a custom admin form, but I am getting an error: django.core.exceptions.FieldError: 'help_num' cannot be specified for Investigation model form as it is a non-editable field This is true, in my model I have it set as such: models.py help_num = models.CharField(max_length=17, unique=True, default=increment_helpdesk_number, editable=False) forms.py class HelpDeskModelForm(forms.ModelForm): class Meta: model = HelpDesk fields = [ "help_num", "help

Making a submit button form field

﹥>﹥吖頭↗ 提交于 2019-12-23 17:29:08
问题 So there exists a snippet here: I was going tor reuse this, but when I declare this in my forms.py, and I render the html.. submit_button = SubmitButtonField() value is not supplied (it's escaped to string None ), while name is default to submit_button , probably because of the forms.Widget constructor. How can I supply the name and the value? I tried to write an init method for the widget... def __init__(self, name, value, label, attrs): print '%s, %s, %s, %s' %(name, value, label, attrs)

using django cleaned data pop to remove data before commit to db

半腔热情 提交于 2019-12-23 14:02:32
问题 I have a form with a select list. When the user selects a value of 8888 or 9999 from the award_grant_type select list, I want some of the data that may or may not exist in the form input fields (the user may have entered data into the form text input fields and then selected 8888 or 9999) to be deleted before the form data is commited to the database. So I have the following model.py code: ..... DISPLAY_ONLY_AWARD_AND_GRANT_DESCRIPTION_WITH_PROMPT = 8888 DISPLAY_ONLY_AWARD_AND_GRANT

using django cleaned data pop to remove data before commit to db

你。 提交于 2019-12-23 14:02:11
问题 I have a form with a select list. When the user selects a value of 8888 or 9999 from the award_grant_type select list, I want some of the data that may or may not exist in the form input fields (the user may have entered data into the form text input fields and then selected 8888 or 9999) to be deleted before the form data is commited to the database. So I have the following model.py code: ..... DISPLAY_ONLY_AWARD_AND_GRANT_DESCRIPTION_WITH_PROMPT = 8888 DISPLAY_ONLY_AWARD_AND_GRANT

Django forms integerField set max_value on runtime

好久不见. 提交于 2019-12-23 12:53:40
问题 I've got a form like this: class SomeForm(forms.Form): first = forms.IntegerField(max_value= DontWantToSetYet) second = forms.IntegerField(max_value= DontWantToSetYet) third = forms.IntegerField(max_value= DontWantToSetYet) How can I set the max_values at runtime? I.E if request.method == 'POST': form = SomeForm(request.POST, max_values = {'first':10,'second':50,'third':666}) [...] 回答1: you can set the max values on fields in the __init__ method, as shown here class SomeForm(forms.Form): def

modelformset __iter__ overloading problem

痞子三分冷 提交于 2019-12-23 12:42:15
问题 I'm writing the custom modelformset. I need that forms to be sorted by value of field "ordering". I overloaded __iter__ method of BaseFormSet in my child formset class. My class inherited from BaseFormSet: class SortedCatForms(BaseFormSet): def __iter__(self): return iter(self.forms.sort( key=lambda form: form['ordering'].value())) #line 38, the problem line. def __getitem__(self, index): return list(self)[index] I use it in my modelformset: OrderCatsFormSet = modelformset_factory

ForeignKey form restrictions in Django

社会主义新天地 提交于 2019-12-23 12:16:50
问题 I'm using Django to write a blog app, and I'm trying to implement a hierarchical category structure. Each category has a "parent" ForeignKey pointing back to the same Category model. I want to allow admins to add categories, and I want the interface to allow them to select a category's parent category. However, I want to avoid I'm-my-own-grandpa situations, so I want to limit the available choices of categories to those which do not have category in question as an ancestor. Right now, I'm

Django unique model fields validation in form

给你一囗甜甜゛ 提交于 2019-12-23 09:26:34
问题 I have a model with a few unique fields and I'm writing a form for it. I found some reference to the [validate_unique][1] method that should check for uniqueness on fields when you call it but my form .is_valid() always returns True . My testcase: class ServerFormTest( TestCase ): def setUp( self ): self.server = Server.objects.create( host = "127.0.0.1", name = "localhost" ) def test_unique_name(self): form = ServerForm({ 'name': 'localhost', 'host': '127.0.0.1' }) self.assertFalse( form.is

File does not upload from web form in Django

夙愿已清 提交于 2019-12-23 09:15:49
问题 Howdy - I've written a very simple app to accept job applications including a resume upload. Running the bundled server for development locally, I can successfully upload files via the web form on the front end and the admin interface. Running it on the remote server (Apache with mod_python) I can successfully upload files via the admin interface but attempts over the web front end yield no uploaded file. I've added FILE_UPLOAD_PERMISSIONS = 0644 to settings, checked the two settings files,