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
Related
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.
This is probably a very newbie question however I'm kind of stuck.
I've been looking around for a way to render a user based view for a calendar scheduler however I can't seem to find anything about user-based views in django. It's probably really simple but I'm too blind to spot any documentation about it. Something alongside of a user profile.
I've read through the django book but I cannot recall if there was anything that describes what I'm trying to achieve. Any hints or links would be a great help.
Thank you.
What you are actually are looking for is a DetailView, Django nowdays uses Class Based Views which are kind of self explanatory, there is a ListView which is used to display a list of objects, a DetailView which is used to display details of a specific object and others like FormViews, CreateView, DeleteView etc (you can find more information to this very detailed site apart from Django's own documentation):
http://ccbv.co.uk/
Since you want to display a view for a specified object, this becomes a DetailView, since a User also has a calendar with events, then you also need to fetch the Calendar with the events (but this all breaks down to how your model relations are glued together).
For instance if your user has a Single Calendar (he...should actually) and many events in the calendar, you could declare a Queryset in the DetailView:
http://ccbv.co.uk/projects/Django/1.6/django.views.generic.detail/DetailView/
You can create common template for all users and then populate it with user specific data.
I'm not really sure where you're stuck, or what you're defining as a "user-based view".
The user model is like any other: you can query it, and other models can be related to it. If you just want to show data for the current user, you get that from request.user: otherwise, you can pass the user ID in the URL like any other parameter.
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
What is the best way to include in a model to save multi-select US states? I need a user to select several states and save this data in a model (via form OR modelform ).
I tried LocalFlavor USStateField model it doesn't seem to work for me since I can't call it like a regular model.
This django app i've written may help you.
I am a django noob and am trying to figure out how to get the admin module to do something slightly different than the normal operation on a single model. Essentially what I need is to run a query and display the results of the query as a view page and then allow the link to the edit page take the user to an existing model's edit view. 2 of the 3 tables in my query are related, but not all 3.
Example:
select a.foo, a.second_field, b.bar, c.unrelated_field
from a, b, c
where a.primary_key = b.foreign_key
and a.some_value = c.some_value
Note that a and c are not defined as related tables.
I would like to have a view of this query output and have a link to the edit view of the b model as a whole when selected.
I have created a view in the DB for this query and simply created a new model which makes it easy to get the view, but I'm not sure this is even the right approach to start with...but from there I can't seem to figure out how to make this link to the edit page for the B table.
Any pointers or advice on how best to accomplish something like this with django admin would be appreciated!
Using Django 1.3.1 by the way.
Cheers!
You can override change_view in your ModelAdmin so it will construct a list of dicts with all your needed data. Then override change_list.html template to display this data correctly and link it with change_form view for correct model. So it will flawlessly integrate in Django's admin site.
And I don't like DB views as long as it's possible to solve the problem without it. If data can be constructed in Python without massive performance gaps and lots of magic code, it should be processed in Python.