A
A
Alexander2013-01-05 16:36:56
Django
Alexander, 2013-01-05 16:36:56

Models in Django without tables in database?

The number of projects written by me in django is not very large yet, so the question arises, perhaps due to a lack of a complete understanding of the framework. Therefore, I would like to hear the opinions of those who know django well.
In the MVC architecture, the model is designed to store the data of an entity from the subject area and to host the business logic applied to this data. In fact, the data is not necessarily rows of database tables. They can also be stored in xml, txt files, they can be obtained through api methods of a remote service.
But in django, for some reason, models are tied specifically to the database. That is, there is no more basic model class (or I don’t know about it) that would have the ability to validate, the ability to create forms based on it, etc., but would not be rigidly tied to the database.
In my case, the application does not store data in the database, but receives everything through the api of the remote soap service. All entities, including users, are obtained and stored by calling api methods. So I was a bit confused as to what to do. As a result, I took django 1.5, because. in earlier versions, it is generally not possible to create your own user model. And I implemented the following solution:
1. I created my own user model, specifying it in the AUTH_USER_MODEL setting, defined managed = False for the model and overridden the save method :

from django.contrib.auth.models import AbstractBaseUser<br>
from django.db import models<br><br>
class User(AbstractBaseUser):<br>
    username = models.CharField(max_length=255, primary_key=True)<br>
    USERNAME_FIELD = 'username'<br><br>
    class Meta:<br>
        managed = False<br><br>
    def save(self, **kwargs):<br>
        # ...<br>
        # код сохранения модели через вызов api<br>
        # ... <br>

2. I wrote my own authentication backend class that works through the soap service api and specified only it in the AUTHENTICATION_BACKENDS setting.
3. For other models, by analogy with the User model, defined managed = False and overridden the save method .
But for some reason, there is a feeling that I am making a crutch. What solution would you suggest in this situation?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
muzhig, 2013-01-05
@muzhig

I think the database is not necessarily SQL. XML and plaintext file can also be considered as a database. The database is, I think, just a repository of serialized model objects. You can always make your own database backend that will save objects in SOAP and send them where necessary.
PS: I think you can take a closer look at django.db.backends.dummy.base.DatabaseWrapper

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question