django-forms

Whats easiest way to use filter_horizontal outside of the Admin in Django

谁说胖子不能爱 提交于 2019-12-30 03:14:29
问题 I have a non-admin form in which I'd like to use filter_horizontal on. I have read this which does much more than what I want (I only want the filter_horizontal). I wanted to check to see if anyone has come up with a simpler (more current) way to just implement the filter_horizontal. So here is the code: class County(models.Model): """County Names""" name = models.CharField(max_length=64) state = USStateField(null=True) class Company(models.Model): """The basics of a company""" name = models

Django Comments: Want to remove user URL, not expand the model. How to?

谁说我不能喝 提交于 2019-12-30 03:03:09
问题 I'm totally understanding the documentation on expanding the Comments app in Django, and really would like to stick with the automatic functionality but ... In the current app, I have absolutely no use for an "URL" to be submitted along with a comment. Being minimally invasive of the default setup, how can I prevent this field from showing up with the comment form ? Using Django 1, or Trunk, and as many generic/built-ins as possible (generic views, default comments set up, etc. I have only a

Django Formset.is_valid() failing for extra forms

雨燕双飞 提交于 2019-12-30 01:52:25
问题 In my Django application application I have a formset that is created from a simple (not-model) form, with the extra=1 (to allow javasript to add more forms later on). class SomeForm(forms.Form): #some fields with required=False length = forms.IntegerField(required=False) # An example of one of the fields with choices i have A = 0 B = 1 C = 2 D = 3 choices = ((A, 'Aah'), (B, 'Baa'), (C, 'Caa'), (D, 'Daa')) # This is a required choice field pickme = forms.ChoiceField(choices=choices)

Model form save. Get the saved object

匆匆过客 提交于 2019-12-30 01:36:10
问题 If I have a model form and save it like: f = FormModel(request.POST) if f.is_valid(): f.save() How can I get back that object that has just been saved? 回答1: When you save a modelform, it returns the saved instance of the model. So all you have to do is assign it to a variable: f = MyModelForm(request.POST) if f.is_valid(): m = f.save() You do not need to mess around with commit=False or any of that stuff unless you are handling more complex data. 回答2: If you know that the model is saved (so

Using a mixin with a Django form class

孤街醉人 提交于 2019-12-30 00:59:33
问题 I'm thinking about creating a mixin form class so that I can add a common set of fields to a variety of otherwise very different forms. Just using it as a base class won't work because I want to be able to use other forms as base classes like so: class NoteFormMixin(object): note = forms.CharField() class MainForm(forms.Form): name = forms.CharField() age = forms.IntegerField() class SpecialForm(MainForm, NoteFormMixin): favorite_color = forms.CharField() My only question is: how does this

Creating one Django Form to save two models

对着背影说爱祢 提交于 2019-12-29 19:21:07
问题 I have the regular Django User model and a UserDetails model ( OneToOneField with User ), which serves as an extension to the User model. (I tried Django 1.5's feature and it was a headache with strangely horrible documentation, so I stuck with the OneToOneField option) So, in my quest to build a custom registration page that will have a registration form comprised of the User fields and the UserDetails fields, I wondered if there was a way to generate the form automatically (with all of its

Django: How to set initial values for a field in an inline model formset?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-29 07:00:52
问题 I have what I think should be a simple problem. I have an inline model formset, and I'd like to make a select field have a default selected value of the currently logged in user. In the view, I'm using Django's Authentication middleware, so getting the user is a simple matter of accessing request.user . What I haven't been able to figure out, though, is how to set that user as the default selected value in a select box (ModelChoiceField) containing a list of users. Can anyone help me with

Django FileField upload is not working for me

孤者浪人 提交于 2019-12-29 05:58:07
问题 I have been scratching my head FileField. Does the FileField require a seperate process? Although my url gets saved .. but my file doesn't get uploaded ... what am i doing wrong? This is my models.py ... class OpLink(models.Model): user = models.ForeignKey(User) file = models.FileField(blank=True, null=True, upload_to="uploads") url = models.URLField(blank=True, null=True) my forms.py class OpLinkForm(ModelForm): class Meta: model = OpLink exclude = ('user') my views.py oplinkform =

Django ModelForm instance with custom queryset for a specific field

南笙酒味 提交于 2019-12-29 03:35:05
问题 I have a model not unlike the following: class Bike(models.Model): made_at = models.ForeignKey(Factory) added_on = models.DateField(auto_add_now=True) All users may work at a number of factories and therefore their user profiles all have a ManyToManyField to Factory . Now I want to construct a ModelForm for Bike but I want the made_at list to consist of only factories at which the current user works. The idea is that users should be able to add bikes that they've assembled and enter which of

use Crispy form with ModelForm

醉酒当歌 提交于 2019-12-29 03:14:30
问题 I've been running into crispy form, and it seems to do exactly what I want: render forms with bootstrap layout. Now, the example talk about using forms.Form . This is ok, I can create mine by writing the code like this: class TemplateCreateForm(forms.Form): title = forms.CharField(label=(u'Task name')) description = forms.CharField(label=(u'Task description')) url_start = forms.CharField(label=(u'Start page url')) url_end = forms.CharField(label=(u'Final page url')) def __init__(self, *args,