I'm building a rather large project, that basically consists of this:
Server 1:
Ice based services.
Glacier2 for session handling.
Firewall allowing access to Glacier2.
Server 2:
Web interface (read, public) for Ice services via Glacier2.
Admin interface for Ice services via Glacier 2.
The point I'm concerned with is the web interface. I want to use Django, because it's both written in python and has that incredibly useful automatic admin panel generator.
The web interface doesn't access any database. It connects to an Ice service on Server #1 via the Glacier2 router and uses the API exposed by those services to manipulate data.
And as you probably know, the admin generation in Django depends on the use of Django's ORM; which I'm not using since I have no database to access.
So I need to generate the admin panel, but, instead of having an standard data access like the ORM normally does, I need to intercept any "db-access" calls and transform them into Ice service calls, and then take the service's output (if any), transform it into whatever the ORM normally returns and return control to Django.
Anyone knows how I could do this? what would I need to subclass? Any specific ideas?
Thanks for your time.
I think there might be a simpler way than writing custom ORMS to get the admin integration you want. I used it in an app that allows managing Webfaction email accounts via their Control Panel API.
Take a look at models.py, admin.py and urls.py here: django-webfaction
To create an entry on the admin index page use a dummy model that has managed=False
Register that model with the admin.
You can then intercept the admin urls and direct them to your own views.
This makes sense if the add/edit/delete actions the admin provides make sense for your app. Otherwise you are better off overriding the admin index or changelist templates to include your own custom actions
The real power of the contrib.admin is django Forms. In essence, the admin tool is basically auto-generating a Form to match a Model with a little bit of urls.py routing thrown in. In the end it would probably just be easier to use django Forms apart from the admin tool.
you can "mock" some class so it look like a model but it does proxy to your APIs
f.e.
class QuerysetMock(object):
def all():
return call_to_your_api()
[...]
class MetaMock(object):
def fields():
return fields_mock_objects..
verbose_name = ''
[...]
class ModelMock(object):
_meta = MetaMock()
objects = QuerysetMock()
admin.site.register(ModelMock)
This may work.. but you need to do a lot django.model compatible stuff
The django ORM has a pluggable backent, which means that you can write a backend for things that aren't RDBMSes. It's probably a rather large task, but a good place to get started is with Malcolm Tredinnick's talk from DjangoCon 2008, Inside the ORM.
Otherwise, you could bypass the ORM altogether, and write the forms manually for the access you need.
Related
This may be an opinionated question but sorry I am too curious.
I learned to develop Django Model-View-Template websites ( multi page websites) and Django Rest Framework.
From the same Django Model can I create Rest API's and MVC templates together ?
I wanted to develop a Blog website that use session authentication and based on MVC architecture. The same server should create API's because the Mobile app for the Blog may consume the API's and use Token Authentication (using Djoser).
If I use same User model for session and token authentication, Can mobile blog app users use their username and password to access website version ?
Django REST Framework is just a collection of helpers to easily create HTTP endpoints that conform to REST behaviour, which mostly means conventions around GET, POST, PUT and DELETE. You could code all this behaviour by hand using default Django, DRF just makes it a lot easier. The end result are simply specific routes which accept input and return output in specific formats to/from models.
Of course you can use that in addition to normal Django Views. It's just a different interface to your models and other business logic. Authentication can be the same, but typically you use some sort of token authentication for the API; that ultimately depends on how the API is supposed to be used exactly.
So I have a django app with some models which I can manipulate via admin and also through a client facing site, like add instances etc. However, I would like to try something a little different from that. I have a Python script that uses POST and JSON objects based on those self same models and I would like to be able to run it, connect to my django app server via a port and create new instances of my models (essentially without using Admin or my client page). Any ideas how I go about this?
If what the script does is simple, it may be easiest just to create a view which receives the JSON, processes it, and returns a JsonResponse.
e.g.
def create_object(request):
if request.method == 'POST':
data = json.loads(request.body)
# Do something with the data - e.g. create a model instance
MyModel.objects.create(**data)
return JsonResponse({'status': 'created'})
Alternatively, if the script does more complicated things or if you intended to expand it in future, you may be best to expose your site via a REST API using something such as Django Rest Framework.
With Django Rest Framework, you can fairly rapidly build CRUD based APIs around your models using components such as ModelViewSets and ModelSerializers and expose them over REST with minimal code.
I'm building a website using django-all auth for it's authentication and social authentication functions. The forms that come bundled with the app are hardly great to look at and hence I decided to create my own views.
The problem is: How do I create them while ensuring that the backend of Django all auth is still available to me? I've dug into the source code and found that it uses class based views for rendering and performing CRUD operations.
I want to know if I can subclass those views in my own app/views.py and just change their template_name field to my own templates. Any advice would be most helpful.
Thanks.
You can of course subclass the views, as long as you change your URLs to point to the overridden versions. However, there is no need to do this just to use your own templates; Django's template loader is specifically written with this use case in mind. Simply create your own directory inside your templates folder to match the one allauth is using, and create your own template files inside it; Django will find yours first and use them.
I am fairly new to Django, though I delved into other web frameworks in the past.
I am in a case where I need to view all instances of a model, be able to interactively sort and filter them through various criterias and execute an arbitratry treatment to the selected objects.
My initial approach would be to use the admin panel, since it seems to provide out of the box the features I'm looking for, plus authentication (as you can guess, I'm trying to set up a back office of some sort).
Nevertheless, I feel admin panels are essentially designed for another kind of use cases, and using them like that would be bad form (heh, "form"... nevermind). The end of this chapter of the Django book seems to confirm this feeling, too.
Is this the right way to go, or should I look into writing something from scratch?
I'd go django admin with no second thoughts. It provides 100% what you're looking for and it suits model-editing applications. Building something from scratch would be an extremely time intensive process. Also your final code would probably do things that admin already does, possibly in a less efficient way.
Even if you have no experience with the django admin application, the learning curve is not as high as developing a new interface + you will know how to use it in future projects.
I would just register the admin site with no admin prefix, and start from there :)
urlpatterns = patterns('',
(r'^', include(admin.site.urls)),
)
Side note: if you want to spice up the admin app you can check out various 3rd party applications that extend it, like grapelli or yawd-admin.
Admin in django is extremely powerful and flexible. You can define custom actions, validations, override the save methods, override default template etc. So it is advisable to modify the admin to suit your needs rather than writing one from scratch.
I am new to using Frameworks for web development and I have noticed that frameworks like django, turbogears etc come with auth packages which contains user models. Am I supposed to directly modify these and use them as my User models or am I supposed to associate my own user models to these and use them just for authentication?
The latter: build a model with a one to one relationship to the User. Don't modify the django one directly or you'll likely run into trouble sooner or later. The django team won't be taking your changes into account after all and you could be adversely impacted if any internal changes are made. (Though you needn't worry about compatibility with the external interface to your own application.)