django-forms

django smart selects on Django version 3.0.1 - error ImportError: cannot import name 'six' from 'django.utils'

ぃ、小莉子 提交于 2020-01-15 01:48:07
问题 Installed django-smart-selects (pip install django-smart-selects) and is not working on django version 3.0.1 I configured using the official installation guide. enter code here $ python manage.py runserver Watching for file changes with StatReloader Exception in thread django-main-thread: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 865, in run self._target(*self._args, **self.

Django - Override data content of a django-tables2 LinkColumn

风流意气都作罢 提交于 2020-01-14 22:42:52
问题 I use django-tables2 LinkColumn to create a column that call a function that allow the export of the object in the table. forms.py: class FilesTable(tables.Table): id = tables.LinkColumn('downloadFile', args=[A('pk')], verbose_name='Export') I would like the content of this column to be the href to downloadFile function with: Export as the text, not the id. 回答1: Something like that should be working ( warning I don't have Python here so it's not tested but you'll get the idea): class

django forms MultipleChoiceField reverts to original value on save

不羁岁月 提交于 2020-01-14 19:03:46
问题 I have wrote a custom MultipleChoiceField. I have everything working ok but when I submit the form the selected values go back to the original choices even though the form validates ok. my code looks something like this: class ProgrammeField(forms.MultipleChoiceField): widget = widgets.SelectMultiple class ProgrammeForm(forms.Form): programmes = ProgrammeField(required=False) def __init__(self, user, *args, **kwargs): self.fields['programmes'].choices = Mymodel.objects.all() self.fields[

django forms MultipleChoiceField reverts to original value on save

末鹿安然 提交于 2020-01-14 19:03:10
问题 I have wrote a custom MultipleChoiceField. I have everything working ok but when I submit the form the selected values go back to the original choices even though the form validates ok. my code looks something like this: class ProgrammeField(forms.MultipleChoiceField): widget = widgets.SelectMultiple class ProgrammeForm(forms.Form): programmes = ProgrammeField(required=False) def __init__(self, user, *args, **kwargs): self.fields['programmes'].choices = Mymodel.objects.all() self.fields[

Django: Set field choices from view?

折月煮酒 提交于 2020-01-14 08:05:29
问题 I've got some <selects> that I need to populate with some choices that depend on the currently logged in user. I don't think this is possible (or easy) to do from inside the form class, so can I just leave the choices blank and set them in the view instead? Or what approach should I take? 回答1: Not sure if this is the best answer, but in the past I have set the choices of a choice field in the init of the form - you could potentially pass your choices to the constructor of your form... 回答2:

Django: Set field choices from view?

跟風遠走 提交于 2020-01-14 08:04:07
问题 I've got some <selects> that I need to populate with some choices that depend on the currently logged in user. I don't think this is possible (or easy) to do from inside the form class, so can I just leave the choices blank and set them in the view instead? Or what approach should I take? 回答1: Not sure if this is the best answer, but in the past I have set the choices of a choice field in the init of the form - you could potentially pass your choices to the constructor of your form... 回答2:

Cleaning data which is of type URLField

[亡魂溺海] 提交于 2020-01-14 04:03:52
问题 I have a simple URLField in my model link = models.URLField(verify_exists = False, max_length = 225) I would like to strip the leading and trailing spaces from the field. I don't think I can do this in "clean_fieldname" or in the "clean" method. Do I need to sub-class the "URLField" and remove the spaces in to_python method? Is there a better way to do this without any sub-classing? Edited This is my form class StoryForm(forms.ModelForm): title = forms.CharField(max_length=225, error_messages

Why “class Meta” is necessary while creating a model form?

这一生的挚爱 提交于 2020-01-13 20:31:49
问题 from django import forms from .models import NewsSignUp class NewsSignUpForm(forms.ModelForm): class Meta: model = NewsSignUp fields = ['email', 'first_name'] here This code works perfectly fine. But, when I remove "class Meta:" as below, it throws a ValueError saying " ModelForm has no model class specified. " from django import forms from .models import NewsSignUp class NewsSignUpForm(forms.ModelForm): model = NewsSignUp fields = ['email', 'first_name'] Can someone please give an

Django model form saving simultaneously post request twice

萝らか妹 提交于 2020-01-13 18:15:11
问题 I am using django-bootstrap-modal-forms 1.3.1 following https://pypi.org/project/django-bootstrap-modal-forms/ but if I run it's project of books it calls the post request to create book twice but saves the from once. As I am using it, it makes twice post request but both the request are saved one with empty file i.e one post saves all the fields of modal like title description , date but not the upload (file) and the next post saves all of it with upload simultaneously This is my model :

Django ModelChoiceField drop down box custom population

拜拜、爱过 提交于 2020-01-13 18:15:09
问题 I have a dropdown box that is being populated by a filtered list of objects from a model "Options". Currently, the dropdown list displays the names of each option. How would I get it to display another attribute from the same table? self.fields['name'] = forms.ModelChoiceField(queryset = Options.objects.filter(option_type = s), label = field_label, required=False) Quick example: drop down box currently displays the names of the cars: "Camero, Nissan, Honda" How would I get it to display the