Defining Models outside models.py

243 views
Skip to first unread message

Sam Taiwo

unread,
Jul 25, 2019, 12:57:59 PM7/25/19
to Django users
Hi everyone,

I know there are currently 3 ways to define a model in Django
1) Define it directly in the models.py file
2) Define it directly in a models package in the same directory as your other app files
3) Define it somewhere completely different and just import the models into the models.py

My question is, is there a way to write a model in any arbitrary file in you directory and link it to your app without having to use models.py file or directory.
For example is there a way to write a model directly in the views file and then add some config to the settings or elsewhere to enable Django to know of it's existence and pick up any changes that happen to it.
I don't mind if it's a complete hack
Thanks

Andréas Kühne

unread,
Jul 29, 2019, 2:22:04 PM7/29/19
to django...@googlegroups.com
Hi,

I definitely wouldn't recommend  doing what you want to do, for several reasons:
1. It will be hard to find the model when you need to update it.
2. Django is built with defaults in mind - and when you go outside the defaults you are working against the framework and not with it.

But that being said, you can probably do what you want with the help of the meta options on a django model:

Specifically the app_label property - you should be able to use. The only thing you need to do then is make sure that the file is "loaded" when django starts up somehow.

Regards,

Andréas


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/95f073eb-8309-4454-8cfd-b190dde90cef%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages