django-forms

How to use a JQuery Datepicker with the Django template language

半城伤御伤魂 提交于 2019-12-20 10:56:11
问题 Looked at this link and found some help but I am wondering how I can choose to use a JQueryUI Datepicker widget for a DateField I have in my models.py models.py from django.db import models class EModel(models.Model): date = models.DateField(blank=False) forms.py from django import forms from models import EModel class EForm(forms.ModelForm): class Meta: model = EModel form.html - How Django renders my form. Not in the admin page <!doctype html> <html lang="en"> <head> <meta charset="utf-8" /

Limit Django's inlineformset_factory to only create new objects

Deadly 提交于 2019-12-20 10:44:21
问题 I am using django's inline formset factory. To use the example in the docs, author = Author.objects.get(pk=1) BookFormSet = inlineformset_factory(Author, Book) formset = BookFormSet(request.POST, instance=author) will create an inline formset to edit books by a particular author. I want to create a formset that only allows users to add new books by that author, not edit existing books. Is there a simple way to use inlineformset_factory to do this? 回答1: inlineformset_factory takes a formset

How to create a list of fields in django forms

ⅰ亾dé卋堺 提交于 2019-12-20 10:41:44
问题 Is there any way to make a django forms class that actually holds an array of fields? I have a database that will pull up a variable number of questions to ask the user and each question will know how to define it's widget...etc, I just can't seem to hook this up to django forms. I tried this: class MyForm(forms.Form): question = [] questions = Question.objects.all() for q in questions: question.append(forms.CharField(max_length=100, label=q.questionText)) But this doesn't seem to expose my

Display some free text in between Django Form fields

倾然丶 夕夏残阳落幕 提交于 2019-12-20 10:37:52
问题 I have a form like the following: class MyForm(Form): #personal data firstname = CharField() lastname = CharField() #education data university = CharField() major = CharField() #foobar data foobar = ChoiceField() Since some fields (like foobar) are populated from the database i can't use another method other than letting Django render it for me with form.as_ul Also i wish i don't have to split the form in multiple forms for ease of mantainance Is there a way to tell Django to display a help

how to add Permissions in Django to Models and Test it using the shell

我的梦境 提交于 2019-12-20 10:06:04
问题 I added the Meta class in my model and synchronized the DB then created an object in the shell it returns false so i really cant understand where is the error or what is missing is there some sort of configuration maybe in some other files .. class Employer(User): # Employer inherits from User employer_verified = models.BooleanField(default=False) class Meta: permissions = ( ("is_member", "Friendly permission description"), ) emp = Employer.objects.create(blablabla) emp.save() emp.has_perm(

Free-form input for ForeignKey Field on a Django ModelForm

孤人 提交于 2019-12-20 09:59:41
问题 I have two models related by a foreign key: # models.py class TestSource(models.Model): name = models.CharField(max_length=100) class TestModel(models.Model): name = models.CharField(max_length=100) attribution = models.ForeignKey(TestSource, null=True) By default, a django ModelForm will present this as a <select> with <option> s; however I would prefer that this function as a free form input, <input type="text"/> , and behind the scenes get or create the necessary TestSource object and then

Editing Django _form.as_p

不想你离开。 提交于 2019-12-20 09:53:10
问题 By default _form.as._p spits out: <p><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></p> Whereas I need <p><label for="id_subject">Subject:</label><p> <input id="id_subject" type="text" name="subject" maxlength="100" /></p> with a break between the label and the input. How can I modify my Django code to do so? 回答1: You simply just can't use form.as_p anymore. If the defaults don't work for you, then you must render the fields

How to send multiple input field values with same name?

这一生的挚爱 提交于 2019-12-20 09:38:33
问题 I have m2m field, lets say it have name 'relations', so i want to allow user to send as many relations as he wants. I add new input to html with javascript with same name, like so <input type='text' name='relations' value='a' /> <input type='text' name='relations' value='b' /> in cleaned_data i receive only value of second input ('b'). How to receive both? 回答1: I don't know how to do that with Forms, but if you want to grab the values in the raw way, here's how I'd do: relations = request

Django - UpdateView with inline formsets trying to save duplicate records?

吃可爱长大的小学妹 提交于 2019-12-20 09:38:03
问题 I have an Expense model and an ExpenseLineItem model. Just like a typical expense/invoice, one expense can have several line items to make up the total cost of an invoice. I'm trying to use class based views to create and update expenses. I've successfully coded the CreateView to make a new expense with multiple expense line items. My problem is when I try and update an existing Expense which already has several expense line items. Here's my code below, and I can't figure out what the issue

Django TextField max_length validation for ModelForm

风格不统一 提交于 2019-12-20 09:29:20
问题 Django does not respect the max_length attribute of TextField model field while validating a ModelForm. So I define a LimitedTextField inherited from the models.TextField and added validation bits similar to models.CharField: from django.core import validators class LimitedTextField(models.TextField): def __init__(self, *args, **kwargs): super(LimitedTextField, self).__init__(*args, **kwargs) self.max_length = kwargs.get('max_length') if self.max_length: self.validators.append(validators