我要建立一个资产管理系统。每个属性都有一个独特的属性 - 代码与部门代码和类型代码相结合。
from django.db import models
class Department(models.Model):
# department_code like rm01
department_code = models.CharField(max_length=4, unique=True)
name = models.CharField(max_length=100)
class Type(models.Model):
# type_code like fe03
type_code = models.CharField(max_length=4, unique=True)
name = models.CharField(max_length=100)
class Property(models.Model):
# property_code like rm01fe037767
property_code = models.CharField(max_length=12, unique=True)
name = models.CharField(max_length=100)
department = models.ForeignKey(Department)
type = models.ForeignKey(Type)
怎么做?或者是否有其他方法可以实现目标?
答案 0 :(得分:0)
是,但不是自动。
在正常情况下:当你想添加新的Propery~>用户应该选择其他2个型号吗?
dep_id = ?
type_id= ?
p = Property (id = dep_id+type_id, name = "blabla"...)
p.save()
And you should define the PK (default is int field, auto incre ~> to charfield) and input it by yourself
通常情况下:我们不这样做......
我们使用:ForeignKey如:
class Item(models.Model):
people = models.ForeignKey(People)
name = models.CharField(max_length=40)
value = models.IntegerField(default=0)
def __str__(self):
return self.name