Python第37天 Form表单一对多操作

文章目录

2019-1-17 22:25:26

Form表单操作

  • 一对多ForeignKey跨表操作
  • views.py内容与之前差不多,主要是修改了表结构(还需修改)
#modles.py
from django.db import models

# Create your models here.
class HostList(models.Model):
    hostname = models.CharField(max_length=32,null=True,verbose_name='主机名')
    ipaddress = models.CharField(max_length=32,null=True,verbose_name='IP地址')
    cpu = models.CharField(max_length=32,null=True,verbose_name='CPU')
    mem = models.CharField(max_length=32,null=True,verbose_name='内存')
    disk = models.CharField(max_length=32,null=True,verbose_name='磁盘')
    system = models.CharField(max_length=32, null=True, verbose_name='操作系统')
    region = models.ForeignKey(to='Region',null=True,verbose_name='区域')
    computer_room = models.ForeignKey(to='ComputerRoom',null=True,verbose_name='机房')
    # application = models.ManyToManyField(to='Application',null=True,verbose_name='应用')

class Region(models.Model):
    name = models.CharField(max_length=32,null=True,verbose_name='区域')
    computer_room = models.ManyToManyField(to='ComputerRoom',null=True,verbose_name='机房')
    def __str__(self):
        return self.name
    class Meta:
        verbose_name_plural = '区域表'

class ComputerRoom(models.Model):
    name = models.CharField(max_length=32,null=True,verbose_name='机房')
    def __str__(self):
        return self.name
    class Meta:
        verbose_name_plural = '机房表'
# class Application(models.Model):
#     name = models.CharField(max_length=16, blank=True, null=True, verbose_name='应用')

class UserInfo(models.Model):
    username = models.CharField(max_length=32,null=True)
    password = models.CharField(max_length=32,null=True)

  • 建立form_class.py,针对各个表单进行创建单独的form类
from django import forms
from django.forms import fields
from django.forms import widgets
from host import models

class UserForm(forms.Form):
    user = fields.CharField(error_messages={'required':'用户名不能为空'})
    # user = forms.CharField()
    pwd = fields.CharField(
        max_length=10,
        min_length=4,
        error_messages={'required':'用户名不能为空'},
        widget=widgets.PasswordInput(attrs={'class': 'form-contorl'})
    )
#注册form
class RegisterForm(forms.Form):
    username = forms.CharField(error_messages={'required': '用户名不能为空'})
    # user = forms.CharField()
    password = forms.CharField(
        max_length=10,
        min_length=3,
        error_messages={'required': '用户名不能为空'})
    pwd2 = forms.CharField(
        max_length=10,
        min_length=3,
        error_messages={'required': '用户名不能为空'})
    def clean(self):
        username = self.cleaned_data.get('username')
        models.UserInfo.objects.filter(username=username)
        try:
            pwd_confirm = self.cleaned_data.get('password')
            if self.cleaned_data.get('pwd2') == self.cleaned_data.get('password'):
                del self.cleaned_data['pwd2']
                return self.cleaned_data
            else:
                from django.core.exceptions import ValidationError
                self.add_error('pwd2', ValidationError('密码输入不一致'))
                return self.cleaned_data
        except KeyError:
            return self.cleaned_data
#资产列表form
class HostForm(forms.Form):
    hostname = fields.CharField(
        required=True,
        widget=widgets.TextInput(attrs={'class':'form-contorl'})
    )
    ipaddress = fields.GenericIPAddressField(
        required=True,
        error_messages={'required': '不能为空','invalid':'输入不合规'},
        widget=widgets.TextInput(attrs={'class': 'form-contorl'})
    )
    cpu = fields.IntegerField(
        required=True,
        widget=widgets.TextInput(attrs={'class': 'form-contorl'})
    )
    mem = fields.CharField(
        required=True,
        widget=widgets.TextInput(attrs={'class': 'form-contorl'})
    )
    disk = fields.CharField(
        required=True,
        widget=widgets.TextInput(attrs={'class': 'form-contorl'})
    )
    system = fields.CharField(
        required=True,
        widget=widgets.TextInput(attrs={'class': 'form-contorl'})
    )
    region_id = fields.ChoiceField(
        required=True,
        # initial=0,
        choices=[],
        widget=widgets.Select(attrs={'class': 'form-control'})
    )
    computer_room_id=fields.ChoiceField(
        required=True,
        # initial=0,
        choices=[],
        widget=widgets.Select(attrs={'class': 'form-control'})
    )

    # computer_room
    def __init__(self, *args, **kwargs):
        super(HostForm, self).__init__(*args, **kwargs)
        # print(models.ComputerRoom.objects.values_list('id', 'name'))
        self.fields['computer_room_id'].choices = models.ComputerRoom.objects.values_list('id', 'name')
        self.fields['region_id'].choices = models.Region.objects.values_list('id', 'name')
        # print('-----------',models.Region.objects.values_list('id', 'name'))

给TA打赏
共{{data.count}}人
人已打赏
开发

Python第47天 权限数据需求分析

2023-9-11 18:33:05

开发

Python第18天 memcached启动脚本v1

2023-9-11 18:33:07

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索