django-forms

django inline formsets with a complex model for the nested form

ぃ、小莉子 提交于 2019-12-18 05:25:08
问题 Say I have django model that looks something like this: class Order(models.Model): number = models... date = models... class OrderLine(models.Model): # One or more lines per order order = models.ForeginKey(Order) common_line = models.OneToOneField(CommonLine) class CommonLine(models.Model): # common elements of what might be on a line item... taxes = model... amount = model... I want to create a form that uses an inlineformset to edit one or more Lines (both OrderLine and CommonLine) per

How do I use UpdateView?

爷,独闯天下 提交于 2019-12-18 05:07:10
问题 I have two, presumably related, problems with UpdateView. First, it is not updating the user but creating a new user object. Second, I cannot restrict the fields displayed in the form. Here is my views.py: class RegistrationView(FormView): form_class = RegistrationForm template_name = "register.html" success_url = "/accounts/profile/" def form_valid(self, form): if form.is_valid: user = form.save() user = authenticate(username=user.username, password=form.cleaned_data['password1']) login(self

How to clear form fields after a submit in Django

喜夏-厌秋 提交于 2019-12-18 02:02:08
问题 I've this: def profile(request, username): if request.method == 'POST': if request.user.is_authenticated(): new_message = Message(author = request.user) form = MessagesForm(request.POST, instance = new_message) else: form = MessagesForm(request.POST) if form.is_valid(): form.save() else: to_user = User.objects.get(username = username) form = MessagesForm(initial = {'user': to_user.pk}) return render(request, "profile.html", {'username': username, 'form': form, 'messages': messages}) This form

django model/modelForm - How to get dynamic choices in choiceField?

人走茶凉 提交于 2019-12-18 00:31:53
问题 i'm experimenting with django and the builtin admin interface. I basically want to have a field that is a drop down in the admin UI. The drop down choices should be all the directories available in a specified directory. If i define a field like this: test_folder_list = models.FilePathField(path=/some/file/path) it shows me all the files in the directory, but not the directories . Does anyone know how i can display the folders? also i tried doing test_folder_list = models.charField(max_length

Django admin, filter objects for inline formset

China☆狼群 提交于 2019-12-17 23:41:24
问题 I've got an inline formset and I would like to exclude some model objects from being displayed in the formset. For eg. there is model B which has foreign key to model A, so it is a 1:n (A object has many B objects) relationship. Now on A admin edit page I've got inlines of B. I wonder if it is possible somehow to filter the list of B objects before the inline formset is rendered, so not all B objects related do A gets into the formset. 回答1: Replying to own question may seem a bit odd but I

django crispy forms: Nesting a formset within a form

人盡茶涼 提交于 2019-12-17 22:51:00
问题 I have a django Formset that I'd like to layout in the middle of another form. I'm using django-crispy-forms to set the layout in the parent form's __init__ : from crispy_forms.helper import FormHelper from crispy_forms.layout import Submit, Layout, Field, Div def __init__(self, *args, **kwargs): self.helper = FormHelper() self.helper.layout = Layout( Div( Div(Field('foo'), css_class='span3'), Div(Field('bar'), css_class='span4'), css_class='row' ), Field('baz', css_class='span1'), ... ) self

Caching queryset choices for ModelChoiceField or ModelMultipleChoiceField in a Django form

爷,独闯天下 提交于 2019-12-17 22:42:28
问题 When using ModelChoiceField or ModelMultipleChoiceField in a Django form, is there a way to pass in a cached set of choices? Currently, if I specify the choices via the queryset parameter, it results in a database hit. I'd like to cache these choices using memcached and prevent unnecessary hits to the database when displaying a form with such a field. 回答1: You can override "all" method in QuerySet something like from django.db import models class AllMethodCachingQueryset(models.query.QuerySet

Updating User model in Django with class based UpdateView

不打扰是莪最后的温柔 提交于 2019-12-17 22:33:24
问题 I am trying to update the Django User model with the class based UpdateView that automatically renders with the current user but am getting an error that a pk or slug is required. The form work and renders with the proper current user context but throws the error when I try to submit the changes. Below is the view I am using: class UserUpdateView(UpdateView): form_class = UserForm model = User template_name = 'members/user_update.html' def get(self, request, **kwargs): self.object = User

Django - Render CheckboxSelectMultiple() widget individually in template (manually)

南笙酒味 提交于 2019-12-17 21:57:49
问题 I have those two models: models.py class App(models.Model): app_name = models.SlugField(max_length=50) options_loaded = models.ManyToManyField(Option) created_by = models.ForeignKey(User) def __unicode__(self): return self.name class Option(models.Model): option_name = models.SlugField(max_length=50) condition = models.BooleanField('Enable condition') option = models.BooleanField('Enable option1') created_by = models.ForeignKey(User) def __unicode__(self): return self.option_name I would like

Django form - set label

社会主义新天地 提交于 2019-12-17 21:48:21
问题 I have a form that inherits from 2 other forms. In my form, I want to change the label of a field that was defined in one of the parent forms. Does anyone know how this can be done? I'm trying to do it in my __init__ , but it throws an error saying that "'RegistrationFormTOS' object has no attribute 'email'". Does anyone know how I can do this? Thanks. Here is my form code: from django import forms from django.utils.translation import ugettext_lazy as _ from registration.forms import