Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django and monkey patching issue

I have recently started experimenting with Django for some web applications in my spare time. While designing the data model for one, I came across the dilemma of using inheritance to define a user of the website or using a technique known as monkey patching with the User class already supplied by the framework.

I tried to add a field by means of (after having defined all my models etc. without errors, according to python manage.py validate):

User.add_to_class('location', models.CharField(max_length=250,blank=True))

and executed the syncdb command. However, I keep getting this error

OperationalError: no such column: auth_user.location

whether I am in the admin view of the site or the manage.py shell. There must be an extra step I'm missing, but there seems to be limited documentation on the whole monkey patching technique. So I'm asking you for assistance before I resort to inheritance. Any code, tips, or pointers to additional documentation are of course welcome.

Thanks in advance.

PS. I'm aware this technique is ugly, and probably ill-advised. ;)

like image 317
Michael Foukarakis Avatar asked Nov 30 '22 06:11

Michael Foukarakis


1 Answers

There's an alternative to both approaches, which is to simply use a related profile model. This also happens to be a well-documented, highly recommended approach. Perhaps the reason that the add_to_class approach is not well-documented, as you noted, is because it's explicitly discouraged (for good reason).

like image 175
ozan Avatar answered Dec 05 '22 03:12

ozan