从标准 Django 模型中删除字段 [英] Delete field from standard Django model

查看:71
本文介绍了从标准 Django 模型中删除字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:这是在 AbstractUser 存在,这可能是您现在想要使用的.

NOTE: this was asked before AbstractUser existed, which is probably what you'd want to use these days.

基本上我想从默认的 Django 用户类中删除默认的电子邮件字段...

Basically I would like to delete the default email field from the default Django User class...

class MyUser(User):
    field = models.CharField(max_length = 10)
    a = 'hello'
    def b(self):
        print 'world'

del User.email
del MyUser.email
del Myuser.field

所有这些都会导致 AttributeError.以下列方式删除方法或属性虽然工作正常:

All these give AttributeError. Deleting methods or attributes in the following way though works fine:

del MyUser.a
del MyUser.b

所以我很好奇为什么这不起作用;这些模型字段是什么类型的东西?

So I'm curious why this doesn't work; what type of things are these model fields?

我尝试的另一件事是通过在 MyUser 中创建一个 email = None 来覆盖电子邮件,但这也不起作用(而且会更难看).

Another thing I tried was overwriting email by creating an email = None in MyUser but that didn't work either (and would be slightly more ugly).

提前致谢!

附言如果你想知道为什么;这更多是出于好奇,而不是应用程序确实需要......但我认为数据库中没有未使用的列是好的.P.P.S.我不想更改 Django 文件以手动删除用户的电子邮件".

P.s. If you are wondering why; it's more for curiousity than that it is really necessary for the application... But I think it's good not to have unused columns in database. P.p.s. I don't want to change the Django files to manually remove 'email' from user.

这里的后续问题(对于那些想做同样事情的人)在syncdb之前,从标准Django模型中删除字段

Follow-up question here (for those who want to do the same thing) Before syncdb, delete field from standard Django model

推荐答案

正如您所发现的,模型字段实际上并不是类属性,即使它们看起来是.

As you've discovered, model fields aren't actually class attributes, even though they appear to be.

模型是由一些非常聪明但复杂的黑客使用元类构建的.当执行模型类定义时(第一次导入它的 models.py),元类会遍历该模型的所有字段定义,并调用每个字段定义的 contribute_to_class 方法.这最终将实际字段放入新类的 _meta.fields 属性中.所以模型类本身没有字段作为直接属性.

Models are constructed by some very clever but complicated hacking around with metaclasses. When a model class definition is executed (the first time its models.py is imported), the metaclass runs through all the field definitions for that model, and calls the contribute_to_class method of each one. This ends up putting the actual fields into the new class's _meta.fields property. So the model class itself doesn't have the fields as direct properties.

然后,当模型实例实际实例化时,Django 获取该列表并直接设置新实例的属性,使用构造函数的参数或字段提供的默认值.因此,通过实例访问的值背后没有实际的字段代码:它是一个简单的实例属性.

Then, when a model instance is actually instantiated, Django takes that list and directly sets the new instance's attributes, using either the arguments to the constructor or the default values supplied by the fields. So, the value that's accessed via the instance has no actual field code behind it: it's a simple instance attribute.

无论如何,这一切的结果是,要从模型定义中删除一个字段,您只需要从 Model._meta.fields 列表中删除它.

Anyway, the upshot of all this is that to remove a field from a model definition you just need to delete it from the Model._meta.fields list.

这篇关于从标准 Django 模型中删除字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆