Django: Include/Exclude Push/Pull Template for ManyToMany Relationship - django

I have a many to many relationship in Django where one lesson may be linked to many (~20) objectives. In total there are over 100 objectives that can be selected that are categorised in two ways. The default combobox size make it difficult to select and view the objectives. Due to the nature of the application (curriculum and lesson planning tool) most of the work is done in admin and it would be useful to view the currently selected objectives better.
I would like something that looked like this:
Link if image not shown
but would settle on viewing more fields at once and/or ideally sorted by those fields selected?
I would be grateful for any help, ideas or examples from the more experience Django developers here. Thanks in advance of your time.
Chris

Luckily, a very similar widget is already provided for use in the admin - just set the filter_horizontal attribute in your admin class:
class MyModelAdmin(admin.ModelAdmin):
model = Lesson
filter_horizontal = ('objectives',)
See the docs.

Related

writing a generic listview template to use across different models

I am new to django and am looking for a generic way of displaying all records of a model in a view by writing minimum html.
So ideally what i would like to do is define my model for example customer, add fields like first name, address, credit card no.
Now i would mark which of these fields are to be rendered ( say publicly_visible = false for credit card field). I repeat this for another model like 'products'.
Next i want my view for customer to render a list of all customer records (the credit card column will not be rendered).
I am wondering if there already is a django package which will do this for me?
The other option which i am trying is to try to use a generic listview to do this but not sure how to proceed.
Meet django's "Class Based Views".
You can read the docs here Django Docs on CBV, there are several generic views to accomplish repetitve tasks.
The view you're looking for regarding your question is called ListView.
For others looking for a good solution to generate read-only list views for your models without writing too much code - This did it for me.
https://github.com/miracle2k/django-tables

Django app where you can send application to authorities

I am currently working to write a web app where people fill out the necessary information, and apply to their mentors.
So, at this point, mentors have a model class that is pretty much like the applicant's, so that they can correct the applicant's info without affecting the applicant's original profile.
I will appreciate any helpful comments. Specifically, I am looking for:
-A similar per-exisiting django app that does more or less so I can browse the source.
-Any special Django feature that allows this that I can not aware of.
-General info on how things like these are done in general.
Thank you.
Ad general info)
You would benefit from doing this in a single model (say ApplicationModel), with fields in pairs - field_name_applicant, field_name_mentor.
Then use a CreateView with its fields property set to only the *_applicant fields for the applicant to fill in the applications initially, and an UpdateView with its fields set to the *_mentor fields for the mentor to correct the applicant fields.
Have ApplicationModel.clean() copy all *_applicant field values to their *_mentor counterpart if the later is not set.
Now you have all your business logic in the model where it belongs; quoting a headline in the introduction of Two Scoops of Django:
Fat Models, Helper Modules, Thin Views, Stupid Templates

Can I make Django admin reflect a hierarchy of models?

Assume a Django application with a few models connected by one-to-many relationships:
class Blog(models.Model):
...
class Post(models.Model):
blog = models.ForeignKey(Blog)
...
class Comment(models.Model):
post = models.ForeignKey(Post)
...
Conceptually, they form a hierarchy, a tree-like structure. I want the Django admin to reflect that. In particular:
in a changelist of posts, every post should have a link to the changelist of corresponding comments;
similarly, a post’s edit page should link to the changelist of comments from the top-right buttons area;
when I open that list of related comments, it needs to reflect the relationship in the breadcrumbs (something like: Posts › “Hello world” › Comments) and, ideally, also in the URL (post/123/comment/).
This should of course also apply to the other levels of the hierarchy.
Number 1 is pretty easy with a custom list_display entry and using the ?post__id= query to the comments changelist. But this is little more than a hack. Generally Django assumes my three models to be independent, top-level entities.
Is there a straightforward way to accomplish this? I guess I could override a bunch of templates and AdminModel methods, but perhaps there is a better solution for what seems like a common situation?
Are you sure you are not just looking at Django Admin Inline Models ?
There is no way that an automated admin will pick up your relationships, because in an RDBS there can be any number of foreign keys / one to one / many to many relations, and Django does not have a customized hierarchical behavior built in.
You can indeed edit the breadcrumb customizing an admin template if you want.
For relations you might also be interested into django MPTT that allows to make hierarchical model instances. Also see this question: Creating efficient database queries for hierarchical models (django) in that respect.
How is this a common situation? Consider the fact a model can have a virtually unlimited number of foreign key relationships, let alone visa versa. How would the admin 'know' how to represent this data the way a user requires without customizing things?
One would suggest you are used to work with content management systems rather than webframeworks (no pun intended). It's important to notice Django isn't a cms, but a webframework you can built on top of as you see fit. In a nutshell: 'Django is rather clueless and unaware of contextual requirements'.
Although the admin is quite a beast out-of-the-box, it can be hard to customize. There have been quite some discussions whether it should even be part of core. I can only suggest, if customizing things tends to get hacky, you should probably write your own 'admin', it's not that hard.

Creating Pages for Django Admin add_form Inline Children

I have a node_user which extends auth.User in my django application. The node_user has many children. When creating a new node_user in the django admin I also provide access to node_users's child objects using the StackedInline sublclass.
However, I want to present the node_user children in a series of pages instead of a stacked one-page list. Is there anyway to spread the children out over pages? Does someone have an example? I think this would help user interaction but I'm unsure how to do it because I'm a newbie.
Thanks
angelo
Django doesn't do this out of the box sorry. You could write a custom admin view but you'd have some tricky decisions to make and coding to do regarding saving. I.e. does hitting 'next' lose changes you made on the first page? My approach to this problem would be to edit the node_user children through their own ModelAdmin - smart use of the list_filter and/or search_fields options (and perhaps list_editable) should make it pretty easy to edit them as a group.

Django admin: Change selected box of related fields to autocomplete

We have some models that are have a user as a foreign key. But with about 25000 users in our system, it's a bit daunting to find the one we need.
Is there a solution out there that's better than the select box? Maybe an autocomplete so we can start typing the user name / address? Or just a search box? When switching the related user for these objects, it's getting harder and harder with 25000 unsorted users.
Even just setting it to sort the users by username would be helpful.
I had this problem and my conclusion was to use an autocomplete field instead. It works pretty well in most cases. The only problem is when you have a lot of entries that are mostly the same. For example, in your case, if you type Robert for the name and there's a few hundred Robert entries in the list...
UPDATE
As mentions in shuckc's answer, Django 2.0+ admin as now autocomplete built in.
For older Django or to use outside of the admin (old answer)
There are many apps that add autocomplete to the Django admin:
django-autocomplete-light
django-extensions (ForeignKeyAutocompleteAdmin)
django-autocomplete (on google code)
django-ajax-selects
django-admin-autocomplete
django-autocomplete (tyrion)
My preferred one is the last one. It's well written, it can be used with the admin and outside of the admin, it works with ManyToManyFields, ForeignKeyFields, CharFields, etc.
I did a fork of this project for my client that adds some niceties like a lookup (loupe) button like the ForeignKeyRawIdWidget.
Django 2.0 admin has autocomplete built in, just set the autocomplete_fields field on the ModelAdmin class. e.g.
class QuestionAdmin(admin.ModelAdmin):
ordering = ['date_created']
search_fields = ['question_text']
class ChoiceAdmin(admin.ModelAdmin):
autocomplete_fields = ['question']
The simplest out-of-the-box solution is to add the field to your ModelAdmin's raw_id_fields -- then you'll get a pop-up window in which you can use the built-in searching/filtering and pagination control's to find and select the object you're after.
If you really want autocomplete, the other answers give a you reasonable starting point.
You can use the ForeignKeyRawIdWidget from django.contrib.admin.widgets. It renders FK relations as an input with a small button along-side which presents a searchable pop up.
There is an app for that (django-autocomplete).