Django and Generic Views - django

I've written an entire app pretty successfully in Django but I have this nagging question that I think I know the answer to but I just want to make sure.
One of the things I really liked about Django was the data model and the ability to not have to do "obvious" stuff. For example, we use the admin interface extensively in our app. The fact that I don't need to write an edit screen for every model and keep it up to date every time the model changes is really nice.
What I'm puzzled by is that I wanted to have one part of the app render "read-only" versions of the models. Essentially I want exactly what I have in the Admin interface but without editable widgets. Now I notice, from the Django code, that that admin interface actually goes through and substitutes the widgets to use the editable ones so I know that non-editable is certainly there.
But as far I can tell, there is no way to just say "render this object" and have Django do the "obvious" thing and render it just like it does for the admin interface but with non-editable fields. I find this hard to believe since it seems like a) this is easier than the admin stuff and b) I know the widgets are already there. But I've looked all over and even the Django examples seem to always create a template and spell out exactly what the page should look like.
Writing a template is probably a good idea in general but early on in development when things are changing it would be better to have something that just does something basic given the information available in the model.
Am I missing something? Sorry if this is a stupid question.

Could be that most non-toy sites want a custom layout/html anyway?
Or, are you looking for Databrowse?

I used something like this: http://www.djangosnippets.org/snippets/937/
There are other similar things around if you google for 'django read-only admin' or similar.
Never underestimate how flexible the Django Admin is...

Related

django: is it possible to use the views that the admin panel uses?

Just learning django here, going through their website's tutorial.
When they take you through their admin panel, it's like, wow, all I have to do is define a model and django will give me a cool web page like this?
But that doesn't seem to be the case--it seems as if all that cool automatic functionality is only inside the admin panel. It seems that, if you want to have a cool add/change form, for instance, a form that looks and acts like the one in the admin panel, you're going to have to do all the layout and cool features by hand.
Am I wrong about this? Is there some way to make something that looks and works exactly like the admin's "change record" panel, but at your own url?
You are right, but I guess your expectations might be wrong.
Django's automatic admin site is one of the things that makes it very powerful. For example if you're building a large site, normally you'd also have to spend a lot of time to create a admin site, I mean A LOT! So Django provides a useful admin site automatically and it does a good job, which is great.
On the other hand your actual site is always going to be different so Django doesn't provide you the similar "automatic" ways as you expect. But it provides you the same building blocks, like ModelForms, ClassBasesView etc.. And of course, you have to put them together. But it's so much easier and faster to build sites with those blocks then doing it by yourself.
That being said, if you really like how admin site is looking and behaving then you can copy and use them on your main site, which will still require you to do some work.
(Couldn't comment, lacking reps)

Single model for storing django application-wide options

I have a set of top-level configuration data fields that I want to be able to set within django admin for each deployment of my django app. I only want one set of these.
Example fields: site_logo, contact_person, address, facebook_url, twitter_url
The problem is that Django Admin is geared towards tables (lists) of models, so its not a good fit for this type of singular configuration model. I really only want one of these models to exist for the whole site, and to be able to click into it from admin and edit the various fields.
It seems i've come across a 3rd party app in the past to accomplish this but can't find it anywhere. Part of the problem is I'm finding it difficult to find the right words to google. Any ideas?
It looks like django-values will do what you're looking for.
Other possible contenders:
http://github.com/sciyoshi/django-dbsettings (doesn't look maintained)
http://github.com/jqb/django-settings
Have a look at django-livesettings it sounds like it might fit.
Not that i have used it, but i have heard good things about django-constance.
And there are even some more options listed in the Configuration-Grid on Django Packages.

Data structure for changing form using Django Models and Admin app

I am writing a Django application and using the built-in ORM. I would like to create functionality to provide a form but the template for that form needs to be built on admin side of the application and can change over time.
For instance, initially they have a form that asks for first name, last name but later if they decide to add birthday field admin should be able to go to this template page add birthrate as new field and on client side, users start seeing it. Is there anything already in place that can be used? I was thinking of using something like this but decided to check here and see if there is something better available.
Is it this what your are looking for?
I have never heard about any library for that, because you have pretty much different approaches to handle that!, but check the link, he is explaining better those approaches.

Should I modify/extend the admin interface, or write my own CRUD views/templates?

I'm trying to write a simple CRM app in Django; partly as a learning exercise and partly for in-house use.
My schema is slightly complex, as rather that have a single Contact model (with a home phone, work phone, home email, etc.), I have stripped down Cntact model plus a Phone model, an Email model, etc., with a ForeignKey pointing back to a Contact. The point is to let Contacts have an arbitrary number of phone numbers, email addresses, etc. Simple, right?
I have some working views and templates for displaying the data - no issues there. And with only a very small amount of poking at admin.py I have a um...eight different TabularInlines set up, and the admin interface works to create and edit the data...but it's ugly and clunky to the point of unusability, and of course there's no conception of permissions or anything. I'm also not really a fan of having a completely different interface for displaying and searching through the data than for editing and adding contacts...I'd like as much as possible to be done inline, so that I can search for a name, look at the record, click "add note", have it popup a form, fill in the details, click submit, and be done, all with AJAXy goodness so there's no page reloads.
Question: Should I plug away at modifying the admin interface to try and make it usable for a user-facing app? And if so, can anyone point me to a good guide or example where someone has really changed the admin interface to make it work for user-facing CRUD operations?
Or should I just go ahead and write my own CRUD views? And if so, can anyone point me to a good guide or example where someone has written custom CRUD views that work with lots of ForeignKeys and inlines? Ideally I want a form that displays a single Contact, all his Email records, plus a blank form to add a new Email record, plus a button to add more blank forms, plus his Phone records, plus a blank form, and so on for all 8 of my associated models.
(Or am I thinking about this all wrong? Any advice appreciated.)
For our intranet, we use ModelAdmin subclasses (not mounted on the admin site via admin.site.register) for most of our C(R)UD views. By using custom templates for the views, it doesn't look like Django admin at all. What is very convenient though, is that it already handles all the validation/saving for us.
In general, I found admin-"hacking" quite useful to quickly write up C(R)UD views and usually with relatively small changes to your ModelAdmin subclass, you can make it work for your use case.
So I'd vote for use ModelAdmin, but not the one you use in admin, hook a different template and come up with some fancy CSS.
I successfully created a software on top of admin.
The admin hooks (these days) allow very fine-grained customizations, i.e. in general you only need to touch what you want to change.
The changes can go from a trivial cosmetic adjustment to a complete swap-out:
If you provide templates/admin/base.html your admin site can look any way you like. And of course, a navigation bar at the top could include links to some of your own views. Watch out not to hardcode URLs in your links, always reverse.
You can overload ModelAdmin's "change_view", "changelist_view" etc. and swap them for your own views. For example I replaced a default changelist and its simple filtering with a search interface that allows dynamic queries to be built, result columns to be customized by the user, and loading/saving of these searches. That didn't affect any of the other views of that ModelAdmin.
Overloading a ModelAdmin's "get_urls()" let's you rewrap existing admin urls to go to your own views. I did the latter for one model where I wanted the simple Add screen to be replaced by a totally customized Wizard (only leaning on ModelForm).
Don't forget the simplest approach, esp. regarding your "AJAXy goodness": Just define "css" and "js" in your ModelAdmin's Meta. Want to move an inline from the bottom to sit between third and fourth field, and that's not possible via parameters? A one-liner in jquery.
Check out "django-grappelli" for an example of how to improve admin look and feel.
What did you mean by "and of course there's no conception of permissions or anything"?

How to approach creating Related Links generic (like Comments/Tags) in Django

Since I have not found a Related Links app that works with Django 1.0/trunk, I was looking to create my own.
I would like to attach "Related Links" to models in the same generic way that Comments framework or Tags work.
I've looked over the Content Types documentation but can't wrap my head around (nor find much documentation for) how to use Generic inline formsets - which is what I'm pretty sure I have to use, but correct me if I'm wrong.
My specific requirement is to be able to relate these "Related Links" to almost any model, and to have the form available outside of the Admin - I'll have logged in members of a certain role adding these links, in my specific case.
I thought about tearing into the source of the Comments app, but I know that it uses special template tags, etc, and I'm just not sure if that'd be overkill for this task.
Looking for links, extra documentation, and possibly even examples of using the generic inline formsets (in Generic Views), or solving the problem in a different way if I'm approaching it wrong.
EDIT: I've used James Bennett's example of Generic Inlines to construct and successfully use those Related Links in the Admin. So the real question is: How do I use James' Related Links outside of the Admin?
You can use django.contrib.contenttypes.generic.generic_inlineformset_factory for that. It has the same interface as inlineformset_factory (with 2 additional parameters: ct_field and fk_field, they can be used to specify your model's contenttype's related field names instead of inlineformset_factory's fk_name).
Documentation for inlineformset_factory can be found here:
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets
Documentation for formsets is also useful.