How to approach creating Related Links generic (like Comments/Tags) in Django - 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.

Related

Creating templates for authentication forms in Django 1.7

First off, I'm very much a newbie when it comes to Django.
The problem I'm struggling with is in trying to create templates for the built-in authentication forms in Django 1.7 but there is very little that I can find in the way of concrete examples anywhere in the documentation or elsewhere.
I can find plenty of questions and examples that describe how to manually create the templates (or copy them from the Django packages) but from what I understand about the Form class and the built-in authentication forms is that I don't need to manually create the actual form in the template. In fact, it seems more desirable to use the Form classes because that would add ensure that fields are named correctly, that the validation such as max length on text fields is applied, etc.
Can anyone point me to some concrete examples or documentation of what I'm talking about? I've read the following sections already,
https://docs.djangoproject.com/en/1.7/topics/forms/
https://docs.djangoproject.com/en/1.7/topics/auth/default/
https://docs.djangoproject.com/en/1.7/topics/auth/customizing/
In fact, I would say that this question extends to any Form class in general but I'm specifically looking at the authentication forms here because I obviously haven't written these Form classes.
I don't think it should make any difference from what I've read since the same process should apply, but I will say that I'm using the django-authtools package (http://django-authtools.readthedocs.org/en/latest/)
I have the actual authentication system working fine. It logs users in and out, I can enforce that certain urls require the user to be logged in first, etc. so its only the actual form display that is an issue.

Django nested formsets snag

As far as I'm aware, even as of Django 1.5, there's no built-in handling of nested formsets - i.e. I have an arbitrary number of groups, to which I must add an arbitrary number of members, all from the same page. I'm currently trying to use Nathan Yergler's method to do so, but it seems to be broken under Django 1.5.
The gist of the method is to override the group formset's add_fields method to include an inline_formset of members. However, when I create a GroupFormSet instance in the view, regardless of whether I've passed any group instances, I get a ValidationError: 'ManagementForm data is missing or has been tampered with'
For example, a snippet from my view's get_context_data:
group_inst = models.TemplateFieldGroup.objects.filter(name="Study")[0]
context['group_formset'] = forms.GroupFormSet(instance=group_inst)
Has anyone successfully deployed this method under Django 1.5, or perhaps does anyone have a better way to accomplish the same goal?
Slight edit: my 'groups' are actually members of an even larger umbrella: a Template object has multiple TemplateFieldGroups which have multiple TemplateFields. However, even passing GroupFormSet() a proper Template instance doesn't solve the issue at hand.
I had a similar issue recently. I solved it by creating some custom form and formset classes based on the StackOverflow answer from: Django admin - inline inlines (or, three model editing at once)
This method worked well for Django 1.4 but stopped working when I updated to Django 1.5. To solve it I created a github repository: https://github.com/didorothy/mlrma
The README.md explains my specific situation and goals more fully. My solution is focused on the Django admin but could be pieced apart and used alone. To do more than three levels it could be extended.
So the final solution to my problem, and unfortunately I can't say I fully understand it, can be found here on Andrei Petre's (old) blog. http://andreipetre.tumblr.com/post/26203496689/nested-formsets-with-django

Use Django admin modules inside own forms

In the Django admin i have a customized changelist with added search and filters. I have been looking alot but cannot seem to find a way to use the whole "changelist module" outside of admin. So i can embed it in one of my own pages.
I do not need any of the authentication or anything like that. I just want to show a table (for a content management backend) that has the nice search, sort and filter capabilities.
Is there perhaps any documentation about doing this?
Of course you can use the ChangeList class for your own projects. I cannot give you a full documentation on doing so here, but some points to start with.
Have a look here to see how the
ChangeList has to be initialized in
your view. (The ChangeList class
lives at
django.contrib.admin.views.main, so
import it from there!)
Look at the admin templates to see how the
corresponding template tags are used.
(also this template)
Maybe you will also find the django.contrib.databrowse-application helpful!

example using django-proxy?

Have you used django-proxy? Can you give me an example of when it would be a good idea to use it? Thanks.
The blog engine Mingus, which I use for my blog, includes django-proxy to allow you to post items of different content types - blog posts, quotes, etc - but have them all appear on the index page in a similar format.
In response to Hank's comment, this is a completely different thing to 'proxy models' - those are for subclassing a model without creating a new table, in order to change some specific functionality rather than any actual fields. This has nothing to do with what django-proxy does.

Django and Generic Views

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...