User-based views in Django 1.6 - django

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.

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

How to add report section to the Django admin?

I want to implement a report section in Django admin. This would mean adding a custom section in the admin homepage where instead of a list of models I would see a list of reports. I want to use Django's admin tables with filters, sorting, everything if possible.
What would be the "best" way of achieving this? I realize this is a "big" question so I'm not asking for code snippets necessarily, a summary of needed actions would be just fine :)
P.S. Be report I mean a "made up" model by custom queries (queryset or how it's called).
P.S.2 Maybe this question should be something like: How to use Django admin tables functionality in own admin view?
P.S.3 Or maybe there is a way of providing to the existing admin interface my own data. This way I don't have to do anything else. I just want to say instead of a model take this data and display it in a nice table which I can sort, filter etc etc.
So you are attempting to add in new pages into the django admin.
This section explains to you exactly how you can do so - https://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-views-to-admin-sites
The basic idea is to add in new urls that you want in your urls.py as if you are adding urls for your "front end" pages. The key difference is that these new urls you are adding should start with ^admin/ and would look something like ^admin/my_special_link_in_admin and this url will point to your own custom view function at a location you so prefer.
E.g.
(r'^admin/my_special_link_in_admin/$', 'my_custom_admin_app.views.special_admin_page'),
So this is the way for complete customization. There's a very good tutorial which I refer to here - http://brandonkonkle.com/blog/2010/oct/4/django-admin-customization-examples/
In addition, if you don't want to do too much work, consider using Django Admin Plus - https://github.com/jsocol/django-adminplus
Or a django-admin-views - https://github.com/frankwiles/django-admin-views

Django: Best way to handle multiselect US state selection

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.

Django admin customization with non-related models

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.

Django Admin: Need to conditionally display fields

What is the best way to conditionally display a field in the admin depending upon the values of other fields?
In particular I'm thinking about the add_form and change_form. Whenever a certain choice is selected I'd like to hide or disable some fields.
I'm thinking that this might require a javascript solution, but am wondering if there is a better (i.e. builtin) way to do this.
Bernhard is right. You might be able to hack the admin view and template to conditionally show/not show widgets for a field, but if you want to do it dynamically based on user behaviors in the admin, you'll be using javascript.
It's not so terrible, though. At least the Django admin templates have model- and instance-specific ids to give you granular control over your show/hide behavior.
My answer may sound esoteric - but I suspect the design direction you may be taking could benefit from some reconsideration. When an interface requires that much TLC to resort to AJAX to make for a superior UX I am inclined to not use Admin interface for that.
My personal view of the Admin interface of Django is that it is a great freebie that gives me an instant CRUD view at a basic level. I would refrain from piggy backing on it for those who need a more user friendly interface (even if some of these users are handling admin functions). You will acquire technical debt in trying to do these type of Ajax UI/UX and complex form validations using the Admin interface.
I think you should be able to create your own ModelForm and specify that the admin uses that (https://docs.djangoproject.com/en/1.7/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form).
Use the _init__() method of the form to selectively display the fields.
I will give it a try and update this answer if it works.
(Actually re-reading the question, it depends. If "values of other fields" is set before the page is loaded, this idea should work. If you want an instant response - click one field and another appears/disappears, yes you will need JavaScript).