濡備綍璇嗗埆Form瀛楁涓竴瀵瑰鎴栬€呭瀵瑰瀛楁
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了濡備綍璇嗗埆Form瀛楁涓竴瀵瑰鎴栬€呭瀵瑰瀛楁相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/nbsp' title='nbsp'>nbsp
cut length ace image choice 瀹氬埗 col fontmodels.py
from django.db import models class Author(models.Model): nid = models.AutoField(primary_key=True) name=models.CharField( max_length=32) age=models.IntegerField() def __str__(self): return self.name class Publish(models.Model): nid = models.AutoField(primary_key=True) name=models.CharField( max_length=32) city=models.CharField( max_length=32) email=models.EmailField() def __str__(self): return self.name class Book(models.Model): nid = models.AutoField(primary_key=True,verbose_name=" 缂栧彿") #瀹氫箟鍦╔admin鏌ョ湅椤甸潰涓樉绀轰腑鏂囧悕绉?ldquo;缂栧彿” title = models.CharField( max_length=32) publishDate=models.DateField() price=models.DecimalField(max_digits=5,decimal_places=2) # 涓嶱ublish寤虹珛涓€瀵瑰鐨勫叧绯?澶栭敭瀛楁寤虹珛鍦ㄥ鐨勪竴鏂?/span> publish=models.ForeignKey(to="Publish",to_field="nid",on_delete=models.CASCADE) # 涓嶢uthor琛ㄥ缓绔嬪瀵瑰鐨勫叧绯?ManyToManyField鍙互寤哄湪涓や釜妯″瀷涓殑浠绘剰涓€涓紝鑷姩鍒涘缓绗笁寮犺〃 authors=models.ManyToManyField(to=鈥?/span>Author鈥?/span>,) def __str__(self): return self.title
admin.py
from django.forms import ModelForm from app01.models import * #涓篵ook琛ㄥ畾鍒秏odelform class BookModelForm(ModelForm): class Meta: model = Book fields = "__all__" labels={ "title":"涔︾睄鍚嶇О", "price":"浠锋牸", "publishDate":"鍑虹増鏃ユ湡" } form = BookModelForm() # 瀹炰緥鍖栦竴涓璞orm # ModelForm浼氭妸model涓嬬殑Foreignkey銆丮anyToMany杞崲涓篺orm涓嬬殑ModelChoiceFiled銆?/span> # ModelMultipleChoiceFiled瀛楁锛屽苟涓擬odelMultipleChoiceFiledid瀛楁缁ф壙ModelChoiceFiled瀛楁 鈥樷€樷€?/span> ChoiceFiled ModelChoiceFiled锛圕hoiceFiled锛?---- select(鍗曢€? ModelMultipleMChoiceFiled 锛圡odelChoiceFiled锛?---select(澶氶€? class Book(model.Model): title = models.CharField( max_length=32) publishDate=models.DateField() price=models.DecimalField(max_digits=5,decimal_places=2) publish=model.Foreignkey(to="Publish") authors=model.ManyToMany(to="Author") from django import forms class BookForm(forms.Form): title = forms.CharField( max_length=32) publishDate=forms.DateField() price=forms.DecimalField(max_digits=5,decimal_places=2) publish = forms.ModelChoiceFiled(to="Publish") authors = forms.ModelMultipleChoiceField(to="Author") 鈥樷€樷€?/span>
views.py
from django.shortcuts import render,HttpResponse # Create your views here. from .admin import form def test(request): for bfield in form: print(bfield锛宼ype(bfield)) #<input type="text" name="title" maxlength="32" required id="id_title" /> <class 鈥榙jango.forms.boundfield.BoundField鈥?gt; # <input type="text" name="publishDate" required id="id_publishDate" /> <class 鈥榙jango.forms.boundfield.BoundField鈥?gt; # <input type="number" name="price" step="0.01" required id="id_price" /> <class 鈥榙jango.forms.boundfield.BoundField鈥?gt; # <select name="publish" required id="id_publish"> # <option value="" selected>---------</option> # </select> <class 鈥榙jango.forms.boundfield.BoundField鈥?gt; # <select name="authors" required id="id_authors" multiple="multiple"> # </select> <class 鈥榙jango.forms.boundfield.BoundField鈥?gt; #鍙戠幇鍏ㄩ儴閮芥槸涓嬮潰杩欑绫诲瀷 #<class 鈥榙jango.forms.boundfield.BoundField鈥?gt;
鎴戜滑鎵撳紑BoundField婧愮爜鐪嬩竴鐪?/p>
from django.forms.boundfield import BoundField
鍙戠幇鏈変竴涓猻elf.field=field,浜庢槸鎴戜滑璇曠潃涓嬮潰鐨勬柟寮忔墦鍗扮湅鐪?/p>
from django.shortcuts import render,HttpResponse # Create your views here. from .admin import form def test(request): for bfield in form:print(bfield.field, type(bfield.field)) #<django.forms.fields.CharField object at 0x00000239D0EDE668>
銆€銆€銆€銆€銆€銆€銆€銆€<class 鈥榙jango.forms.fields.CharField鈥?gt; # <django.forms.fields.DateField object at 0x00000239D0EDE6D8>
銆€銆€銆€銆€銆€銆€銆€<class 鈥榙jango.forms.fields.DateField鈥?gt; # <django.forms.fields.DecimalField object at 0x00000239D0EDE748>
銆€銆€銆€銆€銆€銆€銆€銆€<class 鈥榙jango.forms.fields.DecimalField鈥?gt; # <django.forms.models.ModelChoiceField object at 0x00000239D0EDE7B8>
銆€銆€銆€銆€銆€銆€銆€銆€<class 鈥榙jango.forms.models.ModelChoiceField鈥?gt; # <django.forms.models.ModelMultipleChoiceField object at 0x00000239D0EDE828>
銆€銆€銆€銆€銆€銆€銆€銆€<class 鈥榙jango.forms.models.ModelMultipleChoiceField鈥?gt;
杩欐牱鎴戜滑鍙互鐪嬪嚭鏉ュ摢浜涘瓧娈垫槸涓€瀵瑰鎴栬€呭瀵瑰瀛楁浜嗭紝鎴戜滑鐭ラ亾
ModelMultipleChoiceField缁ф壙浜?span style="background-color: #c0c0c0;">ModelChoiceField锛?br />鎵€浠ュ彲浠ラ€氳繃浠ヤ笅鏂瑰紡杩涜璇嗗埆涓€瀵瑰鎴栬€呭瀵瑰鐨刦orm瀛楁锛?/span>
from django.shortcuts import render,HttpResponse # Create your views here. from .admin import form def test(request): for bfield in form: #閫氳繃浠ヤ笅鏂瑰紡鎴戜滑鍙互鎶奻orm瀛楁涓殑涓€瀵瑰鎴栬€呭瀵瑰瀛楁娣诲姞涓€涓睘鎬s_pop = True #杩欐牱鎴戜滑閫氳繃鍒ゆ柇form鐨勫瓧娈甸噷鏈夋病鏈塱s_pop = True锛岃繘鑰屽氨鑳借瘑鍒玣orm瀛楁涓殑涓€瀵逛竴鎴栬€呭瀵瑰瀛楁 from django.forms.models import ModelChoiceField if isinstance(bfield.field, ModelChoiceField): # 濡傛灉form瀛楁鏄竴瀵瑰鎴栧瀵瑰绫诲瀷 bfield.is_pop = True # 涓篺orm瀛楁鍔犲睘鎬э紝浠ヤ究涔嬪悗鍙栧嚭鍒ゆ柇鏄惁涓轰竴瀵瑰鎴栧瀵瑰瀛楁 return HttpResponse("OK")
以上是关于濡備綍璇嗗埆Form瀛楁涓竴瀵瑰鎴栬€呭瀵瑰瀛楁的主要内容,如果未能解决你的问题,请参考以下文章
濡備綍鍦?Core Web API 涓互涓夌鏂瑰紡杩斿洖鏁版嵁
銆岀26璁层€?濡備綍鐩戞帶鍜岃瘖鏂?JVM 鍫嗗唴鍜屽澶栧唴瀛樹娇鐢紵|瀛︿範绗旇
濡備綍浣跨敤 lftp 鏉ュ姞閫?ftp/https 涓嬭浇閫熷害 | Linux 涓浗