• modelForm


    一 modelForm

    (1) model的知识点:
    class UserInfo(AbstractUser):
    tel=models.CharField(max_length=32)
    gender=models.IntegerField(choices=((1,"男"),(2,"女")),default=1)

    yuan=UserInfo.objects.get(pk=1)
    yuan.get_gender_display()

    (2) modelform使用
    model.py:
    class Book(models.Model):
    nid=models.AutoField(primary_key=True)
    title=models.CharField(max_length=32)
    price=models.DecimalField(max_digits=8,decimal_places=2) # 999999.99
    pub_date=models.DateTimeField() # "2012-12-12"

    # comment_count=models.IntegerField(default=100)
    # poll_count=models.IntegerField(default=100)

    publish=models.ForeignKey(to="Publish",on_delete=models.CASCADE) # 级联删除
    authors=models.ManyToManyField(to="Author")
    def __str__(self):
    return self.title
    form.py:
    # 构建modelform
    class BookModelForm(forms.ModelForm):
    class Meta:
    model=Book
    fields="__all__"

    '''
    BookModelForm等同于:
    class BookForm(forms.Form):
    title=forms.CharField(max_length=32)
    price=forms.IntegerField()
    pub_date=forms.DateField(widget=widgets.TextInput(attrs={"type":"date"}))
    #publish=forms.ChoiceField(choices=[(1,"AAA"),(2,"BBB")])
    publish=forms.ModelChoiceField(queryset=Publish.objects.all())
    authors=forms.ModelMultipleChoiceField(queryset=Author.objects.all())

    '''

    逻辑:
    添加书籍:
    def add(reqeust):
    if GET请求:
    form=BookModelForm()
    return render(reqeust,{"form":form})
    '''
    渲染页面
    <form action="" method="post" novalidate>
    {% csrf_token %}
    {% for field in form %}
    <div class="form-group">
    <label for="title">{{ field.label }}</label>
    {{ field }}
    <span>{{ field.errors.0 }}</span>
    </div>
    {% endfor %}
    <input type="submit" value="提交" class="btn btn-default pull-right">
    </form>
    '''
    else POST请求:
    form=BookModelForm(request.POST)
    if form.is_valid():
    form.save() # Book.objects.create(clean_data)
    return redirect("/")
    else:
    return render(reqeust,{"form":form})



    编辑书籍:

    def edit(request,id):
    edit_obj=Book.objects.get(pk=id)
    if GET请求:
    form=BookModelForm(instance=edit_obj)
    return render(reqeust,{"form":form})
    '''
    渲染页面同添加页面

    '''
    else POST请求:
    form=BookModelForm(request.POST,instance=edit_obj)
    if form.is_valid():
    form.save() # edit_obj.update(clean_data)
    return redirect("/")
    else:
    return render(reqeust,{"form":form})


    今天博客:
    https://www.cnblogs.com/yuanchenqi/articles/7614921.html
    https://www.cnblogs.com/yuanchenqi/articles/8034442.html#_label1

  • 相关阅读:
    单相、二相、三相区别
    Live for Speed 车模、赛道模型导出
    Lenovo/IBM Thinkpad X41 Tablet
    科普题外话 赛车性能的关键指标: 马力和扭力
    MAME™ Official Developer Documentation!
    Visual Studio 2005 & SQL Server 2005 are COMING!
    星际争霸(Star Craft)的Sprites导出
    推荐一本关于操作系统实践的好书
    科普题外话:Experimental Advanced Superconducting Tokamak - 人造太阳
    The Space Elevator -通往地球同步轨道的天梯
  • 原文地址:https://www.cnblogs.com/duhong0520/p/11805203.html
Copyright © 2020-2023  润新知