Specific models without a database table

4,333 views
Skip to first unread message

Kristofer Pettijohn

unread,
Aug 19, 2011, 9:33:08 PM8/19/11
to django...@googlegroups.com
Hello,

Is it possible to create specific models without a database table?

Basically what I would like to do is create an email account management application that ties into my existing mail server and its API.  I would like Django to have a Users model and keep track of users, a Domains model to keep track of the email domains for the user, but I don't want it to actually keep track of email addresses.  Once the user is in the application, they will go into the "EmailAccount" model and I simply want the model to query my mail server via its SOAP API.  So when they create/delete/edit email accounts, there will be form pages and simple validation done by Django, but the actual work will be done by connecting to the mail servers API and not a database.

Is this possible?

Thanks,
Kris

Malcolm Box

unread,
Aug 20, 2011, 6:51:23 AM8/20/11
to django...@googlegroups.com
On 20 August 2011 02:33, Kristofer Pettijohn <kris...@cybernetik.net> wrote:
Hello,

Is it possible to create specific models without a database table?


Yes, it's possible. You want the "managed" attribute on the model - see https://docs.djangoproject.com/en/1.3/ref/models/options/#managed

This will prevent Django from creating or modifying the db table
 
Basically what I would like to do is create an email account management application that ties into my existing mail server and its API.  I would like Django to have a Users model and keep track of users, a Domains model to keep track of the email domains for the user, but I don't want it to actually keep track of email addresses.  Once the user is in the application, they will go into the "EmailAccount" model and I simply want the model to query my mail server via its SOAP API.  So when they create/delete/edit email accounts, there will be form pages and simple validation done by Django, but the actual work will be done by connecting to the mail servers API and not a database.
 
Is this possible?


That is substantially harder, but could be possible. The main problem is that the Django ORM will want to write SQL queries when there's a link to the EmailAccount model.

Your best bet is probably a proxy model that contains a reference to the relevant API record (e.g. the email address identifier or whatever the API uses), and then a custom save() method that writes the values out to the DB. You can use the Django form logic etc without it needing to be backed by a model.

It will largely depend on how you want the EmailAccount to look - the closer you want it to work to a standard ORM model, the more work you'll have to do to trick things. If it's a simple field that isn't used for queries, then you could look at creating a custom field type that knows how to read/write the values to the API.

All the above advice is worth exactly what you paid for it!

Malcolm

sed...@gmail.com

unread,
Aug 20, 2011, 12:44:33 PM8/20/11
to Django users
Hi,
you may use custom model manager (responsible for retrieving objects
from DB) and custom save method (which would not call Model.save),
together with managed=True.

You may still face some issues, however I think this should be
possible.

Cheers,
Filip

On 20 srp, 12:51, Malcolm Box <malcolm....@gmail.com> wrote:
> On 20 August 2011 02:33, Kristofer Pettijohn <kristo...@cybernetik.net>wrote:
>
> > Hello,
>
> > Is it possible to create specific models without a database table?
>
> Yes, it's possible. You want the "managed" attribute on the model - seehttps://docs.djangoproject.com/en/1.3/ref/models/options/#managed

Kristofer Pettijohn

unread,
Aug 21, 2011, 10:11:44 PM8/21/11
to django...@googlegroups.com
Great, thank to both of you.

I'm coming from a world of CakePHP. I did more Python programming back in the day, and am getting back into it, so I'm trying to get more familiar with Django.

Thanks for the suggestions, I'll see what I can make happen.

Cheers,
Filip

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Kristofer Pettijohn

unread,
Aug 21, 2011, 11:29:33 PM8/21/11
to django...@googlegroups.com
> That is substantially harder, but could be possible. The main problem is that the Django ORM will want to write SQL queries when there's a link to the EmailAccount model.
>
> Your best bet is probably a proxy model that contains a reference to the relevant API record (e.g. the email address identifier or whatever the API uses), and then a custom save() method that writes the values out to the DB. You can use the Django form logic etc without it needing to be backed by a model.
>
> It will largely depend on how you want the EmailAccount to look - the closer you want it to work to a standard ORM model, the more work you'll have to do to trick things. If it's a simple field that isn't used for queries, then you could look at creating a custom field type that knows how to read/write the values to the API.

Are there any examples out there that get me started for overriding the save() method?

Ideally, I'd like to create fields in the Model that will similarly match fields in the mail server's API, so I can do queries, updates, etc. I realize that I will need to do a lot of trickery to try to bring it closer to the standard ORM model. So any little examples that might be out there would be helpful.

> All the above advice is worth exactly what you paid for it!

Thanks again!

bruno desthuilliers

unread,
Aug 22, 2011, 8:53:22 AM8/22/11
to Django users
On 20 août, 12:51, Malcolm Box <malcolm....@gmail.com> wrote:
> On 20 August 2011 02:33, Kristofer Pettijohn <kristo...@cybernetik.net>wrote:
>
> > Hello,
>
> > Is it possible to create specific models without a database table?
>
> Yes, it's possible. You want the "managed" attribute on the model - seehttps://docs.djangoproject.com/en/1.3/ref/models/options/#managed
>
> This will prevent Django from creating or modifying the db table


It won't make the model work without the corresponding DB table.

bruno desthuilliers

unread,
Aug 22, 2011, 8:57:43 AM8/22/11
to Django users
On 20 août, 03:33, Kristofer Pettijohn <kristo...@cybernetik.net>
wrote:
> Hello,
>
> Is it possible to create specific models without a database table?


Depends on your definition of "models". If you mean "django.db.Model"
subclasses, nope - this part is nothing but a layer between your app
and the database. Now you can obviously write "domain model" classes
that are not tied to a relational DB, but you won't have the
querying / persistance support provided by Django's ORM.
Reply all
Reply to author
Forward
0 new messages