濡備綍璇嗗埆Form瀛楁涓竴瀵瑰鎴栬€呭瀵瑰瀛楁

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了濡備綍璇嗗埆Form瀛楁涓竴瀵瑰鎴栬€呭瀵瑰瀛楁相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/nbsp' title='nbsp'>nbsp   cut   length   ace   image   choice   瀹氬埗   col   font   

models.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

鎶€鏈浘鐗? src=  

 鍙戠幇鏈変竴涓猻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 涓浗

濡備綍鑾峰彇涓€涓暟鎹簱鐨勬墍鏈夊缓琛ㄨ鍙ヤ笌鍒涘缓绱㈠紩鐨勮鍙ワ紵

濡備綍灏嗘暟鎹粠MySQL/MongoDB涓縼绉昏嚦浜戝紑鍙戞暟鎹簱