django-forms

How to subclass django's generic CreateView with initial data?

守給你的承諾、 提交于 2019-12-29 03:12:26
问题 I'm trying to create a dialog which uses jquery's .load() function to slurp in a rendered django form. The .load function is passed the pk of the "alert" object. Also available in the class functions are things like self.request.user so I can pre-fill those fields, shown below in the Message model (models.py): class Message(models.Model): user = models.ForeignKey(User) alert = models.ForeignKey(Alert) date = models.DateTimeField() message = models.TextField() Subclassing django's CreateView

Different initial data for each form in a Django formset

泄露秘密 提交于 2019-12-29 02:51:37
问题 Is it possible to prepopulate a formset with different data for each row? I'd like to put some information in hidden fields from a previous view. According to the docs you can only set initial across the board. 回答1: If you made the same mistake as me, you've slightly mistaken the documentation. When I first saw this example... formset = ArticleFormSet(initial=[ {'title': 'Django is now open source', 'pub_date': datetime.date.today(),} ]) I assumed that each form is given the same set of

Django 2 models 1 form

独自空忆成欢 提交于 2019-12-28 19:00:14
问题 So, I'm still a total noob at Django and I was wondering how to do the following: So lets say that I have something like the below code: class UserProfile(models.Model): #Some fields class UserProfileOther(models.Model): #Some other fields that I want in another table for organization user_profile = models.OneToOneField(UserProfile) How do I now create a form that includes both of the above models? 回答1: You can create two separate ModelForm classes. But in your view, you have to add a prefix

How to make FileField in django optional?

和自甴很熟 提交于 2019-12-28 12:15:09
问题 I have form with a textbox and filefield in django. It should let the use either paste the text into that box or upload a file. If the user has pasted the text into the box, I needn't check the fileField. How do I make the forms.FileField() optional? 回答1: If you're using a forms.FileField() in a forms.Form derived class, you can set: class form(forms.Form): file = forms.FileField(required=False) If you're using a models.FileField() and have a forms.ModelForm assigned to that model, you can

Django: how to change the choices of AdminTimeWidget

一曲冷凌霜 提交于 2019-12-28 11:48:11
问题 The AdminTimeWidget rendered in admin for a DateTimeField displays an icon of a clock and when you click you have the choice between: "Now Midnight 6:00 Noon". How can I change these choices to "16h 17h 18h"? 回答1: Chris has a great answer. As an alternative you could do this using just javascript. Place the following javascript on the pages where you want the different time options. DateTimeShortcuts.overrideTimeOptions = function () { // Find the first time element timeElement = django

Django form with choices but also with freetext option?

回眸只為那壹抹淺笑 提交于 2019-12-28 08:10:08
问题 What I'm looking for: A single widget that gives the user a drop down list of choices but then also has a text input box underneath for the user to enter a new value. The backend model would have a set of default choices (but wouldn't use the choices keyword on the model). I know I can (and I have) implemented this by having the form have both a ChoicesField and CharField and have code use the CharField if ChoicesField is left at the default, but this feels "un-django" like. Is there a way

Django forms: how to dynamically create ModelChoiceField labels

风格不统一 提交于 2019-12-28 06:05:24
问题 I would like to create dynamic labels for a forms.ModelChoiceField and I'm wondering how to do that. I have the following form class: class ProfileForm(forms.ModelForm): def __init__(self, data=None, ..., language_code='en', family_name_label='Family name', horoscope_label='Horoscope type', *args, **kwargs): super(ProfileForm, self).__init__(data, *args, **kwargs) self.fields['family_name'].label = family_name_label . . self.fields['horoscope'].label = horoscope_label self.fields['horoscope']

Django forms: how to dynamically create ModelChoiceField labels

人走茶凉 提交于 2019-12-28 06:03:48
问题 I would like to create dynamic labels for a forms.ModelChoiceField and I'm wondering how to do that. I have the following form class: class ProfileForm(forms.ModelForm): def __init__(self, data=None, ..., language_code='en', family_name_label='Family name', horoscope_label='Horoscope type', *args, **kwargs): super(ProfileForm, self).__init__(data, *args, **kwargs) self.fields['family_name'].label = family_name_label . . self.fields['horoscope'].label = horoscope_label self.fields['horoscope']

Sending request.user object to ModelForm from class based generic view in Django

[亡魂溺海] 提交于 2019-12-28 05:33:10
问题 So, my goal is to be able to filter a ModelChoiceField queryset in my ModelForm to only include Places that request.user has created. My ModelForm is simply: class PlaceEventForm(models.ModelForm): class Meta: model = Event I'd like to be able to add something like: def __init__(self, *args, **kwargs): super(PlaceEventForm, self).__init__(*args, **kwargs) self.fields['place'].queryset = Place.objects.filter(created_by=request.user) However, I can't seem to find a way to access the request in

Define css class in django Forms

最后都变了- 提交于 2019-12-27 12:40:10
问题 Assume I have a form class SampleClass(forms.Form): name = forms.CharField(max_length=30) age = forms.IntegerField() django_hacker = forms.BooleanField(required=False) Is there a way for me to define css classes on each field such that I can then use jQuery based on class in my rendered page? I was hoping not to have to manually build the form. 回答1: Yet another solution that doesn't require changes in python code and so is better for designers and one-off presentational changes: django-widget