modelform

How to create a subform for Django ModelForm

半世苍凉 提交于 2020-05-14 07:09:33
问题 I'm having a problem to add a ManyToMany field with ModelForm, the problem is that I don't know how to create a subform to add this data to my primary form. I want to create a subform to be saved when I click a button, and I want the data that was saved to be selected to use in the primary form. my model class Teste(models.Model): name = models.CharField(max_length=255) parent_institution_name = models.CharField(max_length=255) laboratory_departament = models.CharField(max_length=255, null

django insert data from 2 tables using a single ModelForm

∥☆過路亽.° 提交于 2020-01-16 19:25:35
问题 I want to get data from 2 tables into a form using ModelForm, like this: fist model: class Cv(models.Model): created_by = models.ForeignKey(User, blank=True) first_name = models.CharField(('first name'), max_length=30, blank=True) last_name = models.CharField(('last name'), max_length=30, blank=True) url = models.URLField(verify_exists=True) picture = models.ImageField(help_text=('Upload an image (max %s kilobytes)' %settings.MAX_PHOTO_UPLOAD_SIZE),upload_to='avatar') bio = models.CharField((

Save modelForm to update existing record

有些话、适合烂在心里 提交于 2020-01-02 16:18:09
问题 I create a modelForm with instance to existing model (Book). I am not able to update the Books record. Adding a new record is fine but when I attempt to update, it appears to be unable to find the publisher (which is a foreign key). Error is "No Publisher matches the given query." models.py class Publisher(models.Model): name = models.CharField(max_length=30) address = models.CharField(max_length=50) city = models.CharField(max_length=60) state_province = models.CharField(max_length=30)

How add a 'Star *' after a django ModelForm CharField?

◇◆丶佛笑我妖孽 提交于 2020-01-01 10:13:30
问题 i have some necessary fields in my django ModelForm. How can i add a red star (*) after the required fields ? 回答1: I'm going to assume you want this to happen automatically, so here's one of a few different ways: {% for field in form %} <label for="{{ field.auto_id }}">{{ field.label_tag }} {% if field.field.required %} <span class="required">*</span> {% endif %} </label> {% endfor %} Then you can style the asterisk using CSS. Or, you can add the asterisk using CSS instead if you want: <style

add extra field to ModelForm

房东的猫 提交于 2020-01-01 07:52:42
问题 I am adding an extra field to a Django ModelForm like that: class form(forms.ModelForm): extra_field = forms.CharField(label='Name of Institution') class Meta: model = db_institutionInstitution fields = ['conn_kind','time','inst_name2'] The form is actually working fine, but I cant prepopulate it. I use it in a modelformset_factory : formset = modelformset_factory(db_institutionInstitution,form=form) I manually run through a queryset and add the entry in the dictionary needed for the

add extra field to ModelForm

不问归期 提交于 2020-01-01 07:51:48
问题 I am adding an extra field to a Django ModelForm like that: class form(forms.ModelForm): extra_field = forms.CharField(label='Name of Institution') class Meta: model = db_institutionInstitution fields = ['conn_kind','time','inst_name2'] The form is actually working fine, but I cant prepopulate it. I use it in a modelformset_factory : formset = modelformset_factory(db_institutionInstitution,form=form) I manually run through a queryset and add the entry in the dictionary needed for the

Django model form save (multi database) question

你说的曾经没有我的故事 提交于 2019-12-30 13:28:03
问题 another noob Django question. The below both work for me, is there any difference or anything I should be aware of ? I'm using Django 1.2.5. Thanks. o = Staff() form = StaffForm(request.POST, instance=o) if form.is_valid(): o.save(using='dbName') o = Staff() form = StaffForm(request.POST, instance=o) if form.is_valid(): f = form.save(commit = False) f.save(using='dbName') 回答1: The first example does not work - it doesn't update the instance from the form. Use the second. 来源: https:/

Django model form save (multi database) question

穿精又带淫゛_ 提交于 2019-12-30 13:27:39
问题 another noob Django question. The below both work for me, is there any difference or anything I should be aware of ? I'm using Django 1.2.5. Thanks. o = Staff() form = StaffForm(request.POST, instance=o) if form.is_valid(): o.save(using='dbName') o = Staff() form = StaffForm(request.POST, instance=o) if form.is_valid(): f = form.save(commit = False) f.save(using='dbName') 回答1: The first example does not work - it doesn't update the instance from the form. Use the second. 来源: https:/

Access request.user in modelForm

﹥>﹥吖頭↗ 提交于 2019-12-30 11:06:16
问题 If i am using model forms where my views.py looks like: def dog_image_upload(request): if request.user.is_authenticated(): if request.method == 'POST': form = DogImageForm(request.POST, request.FILES) if form.is_valid(): form.save() else: form = DogImageForm(user) return render_to_response("dog-image-upload.html", {'form': form}, context_instance=RequestContext(request)) else: return HttpResponseRedirect('/') And in model.py i want to do this: class DogImageForm(ModelForm): dogs = forms

Using class based generic view DetailView with a ModelForm reveals a bug - how to proceed?

邮差的信 提交于 2019-12-29 07:42:32
问题 I've been impressed how rapidly a functional website can go together with generic views in the tutorials. Also, the workflow for form processing is nice. I used the ModelForm helper class to create a form from a model I made and was delighted to see that so much functionality came together. When I used the generic list_detail.object_detail I was disappointed that all that I could display were fields individually. I knew the ModelForm class contained information for rendering, so I wanted to