editable models (db schema) for admin (no developer)

23 views
Skip to first unread message

Thomas Güttler

unread,
Jun 3, 2014, 9:58:47 AM6/3/14
to django...@googlegroups.com
Hi,

I am a software developer and like the way django ORM defines the database (via models.py) very much.

Unfortunately for a new project, we have the constraint, that the admin must be able
to add some columns. The admin is not a developer, and he only uses a web interface.

I don't want to make model.py files editable via an admin interface, since running migrations and all
that does not fit into "web editable".

Any hints how to solve this?

Thomas



Erik Cederstrand

unread,
Jun 3, 2014, 10:28:32 AM6/3/14
to Django Users
Are you *really* sure you want to do this? Monkey-patching running code, adding class attributes and inserting database columns on the fly to a production system isn't something recommended in textbooks. Try describing your situation a bit. Maybe you could use a key-value approach and m2m relations instead?

If you *really* want to do it, and you want to edit models.py, then you should schedule an external task that appends the column to models.py (and commits the change to your VCS, adds tests, ...) and deploys the new code on your server.

Another possibility is that you avoid editing models.py by creating the new database column manually using raw SQL, so Django doesn't know about it. Then override __getattr__ on the model so my_obj.extra_col_123 circumvents the ORM and fetches data by other means, or simply issue raw SQL when you need to access 'extra_col_123' in your code.

Erik

Thomas Güttler

unread,
Jun 3, 2014, 10:34:38 AM6/3/14
to django...@googlegroups.com


Am 03.06.2014 16:27, schrieb Erik Cederstrand:
> Den 03/06/2014 kl. 15.57 skrev Thomas Güttler <h...@tbz-pariv.de>:
>
>> I am a software developer and like the way django ORM defines the database (via models.py) very much.
>>
>> Unfortunately for a new project, we have the constraint, that the admin must be able
>> to add some columns. The admin is not a developer, and he only uses a web interface.
>>
>> I don't want to make model.py files editable via an admin interface, since running migrations and all
>> that does not fit into "web editable".
>
> Are you *really* sure you want to do this? Monkey-patching running code, adding class attributes and inserting database columns on the fly to a production system isn't something recommended in textbooks. Try describing your situation a bit. Maybe you could use a key-value approach and m2m relations instead?

I am sure: I don't want it. I guess you have read my post too fast.

> Another possibility is that you avoid editing models.py by creating the new database column manually using raw SQL, so Django doesn't know about it. Then override __getattr__ on the model so my_obj.extra_col_123 circumvents the ORM and fetches data by other means, or simply issue raw SQL when you need to access 'extra_col_123' in your code.

Yes, this would be a solution.

Thank you for your feedback.

Thomas Güttler

unread,
Jun 4, 2014, 6:34:57 AM6/4/14
to django...@googlegroups.com
I found several solution here: http://stackoverflow.com/a/7934577/633961
Reply all
Reply to author
Forward
0 new messages