简体   繁体   English

如何在Django中使数据库字段不可更改?

[英]How can I make databasefields unchangeable in Django?

Let's say I have Cars , CarTypes , and their BuildDates like this: 假设我有CarsCarTypes和它们的BuildDates如下所示:

class Car(db.models):
    licence_name = models.CharField(max_length=64)
    car_type = models.ForeignKey(Type)

class Type(db.models):
    name = models.CharField(max_length=64)
    build_during = models.ForeignKey(BuildDate)   

class BuildDate(db.models):
    begin = models.DateField(null=True)
    end = models.DateField(null=True)

    class Meta(object):
        unique_together = (('begin', 'end'),)

I want to have the BuildDates in a seperate table such I can do faster queries and other stuff with Django-external tools. 我想将BuildDates在一个单独的表中,以便可以使用Django外部工具进行更快的查询和其他操作。

Since BuildDates are unique but shared between CarTypes , I want to make sure this: If the BuildDate d0 of one CarType t1 is changed, the other CarTypes are not affected and they still share the identical BuildDate d0 – but not with t1 which now has d1 . 由于BuildDates是唯一的但在CarTypes之间CarTypes ,因此我想确保:如果BuildDate了一个CarType t1BuildDate d0 ,则其他CarType不会受到影响,它们仍共享相同的BuildDate d0 ,但不会与现在拥有d1 t1共享。 。 I believe it is necessary to create a new BuildDate d1 for that CarType t1 . 我认为有必要为该CarType t1创建一个新的BuildDate d1 Is there a recommended or recommendable way to do this? 是否有推荐或推荐的方式来做到这一点?

Use "editable" attribute. 使用“可编辑”属性。 When you set "False", element will not be shown in the admin, that is, you can't change its value. 设置为“ False”时,元素将不会显示在管理员中,也就是说,您无法更改其值。

eg 例如

licence_name = models.CharField(max_length=64, editable=False) licence_name = models.CharField(max_length = 64,editable = False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM