3

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

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

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?

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).

Thanks in advance!

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.

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

Community
  • 1
  • 1
Mark
  • 18,730
  • 7
  • 107
  • 130

2 Answers2

7

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

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.

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.

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.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • This is interesting, why do they do all this hacking? – Timmy O'Mahony Nov 23 '11 at 23:05
  • 2
    To enable the nice declarative syntax. Otherwise the fields really would be class attributes, so if you set the value on one instance it would carry over to all of them - not very useful. – Daniel Roseman Nov 23 '11 at 23:15
  • 1
    Little follow-up question: if I remove a field from class A() before creating class B(A), B still gets the deleted A fields... Any idea why? Thanks in advance! – Mark Dec 10 '11 at 21:22
  • I'm not sure if it's due to changes since 2011 or because it's a many2many field, but as a warning to others, this doesn't work in 1.8 for m2m fields. – Mark Sep 01 '15 at 22:15
0

Since Model._meta.fields is an immutable list, you won't be able to change it directly.

You can, however, modify local_fields like this:

def remove_field(model_cls, field_name):
    for field in model_cls._meta.local_fields:
        if field.name == field_name:
            model_cls._meta.local_fields.remove(field)

remove_field(User, "email")
user2468842
  • 183
  • 1
  • 5