I have a django project in which the the form records the name and the email addresses of the users and I was able to put that data using forms.py and models.py.
What I want to do nextis to create an action through which I can download that in csv file.
My admin page looks like this now and I want to add action right above.
in order to add an action to a model in the admin page you have to create a new class like this and register it with your model:
admin.py
from youSite.views import downloadCSV
from yourSite.models import Info
class infoAdmin(admin.ModelAdmin):
actions =[downloadCSV]
admin.site.register(infoObject, infoAdmin)
You have to create the function in your views and import it into the admin page. It create a new action in that model.
Hope it helps
Related
I have implemented Historical tracking of changes to objects in Django using django-simple-history
https://django-simple-history.readthedocs.io/en/2.10.0/index.html
I have mentioned a field history in Model using which I can query an object's history.
history = HistoricalRecords()
There us also a table created in the database with name appname_historicalmodelname.
I want to display this table appname_historicalmodelname in django admin where in we have list of records sorted by history_time.
As I don't have a Model class for that History table, I'm unable to use admin.site.register(HistoricalModelName). How can I display this table in Django admin site?
Django: 1.11
Python: 2.7
django-simple-history comes with SimpleHistoryAdmin, which bolts on a "History" button to your normal admin view that allows you to see the historical records for a particular model instance. However, if you want to be able to create a django admin view for the historical records themselves, you can do the following (assuming base model CustomModel in app my_app):
from django.apps import apps
from django.contrib import admin
HistoricalCustomModel = apps.get_model("my_app", "HistoricalCustomModel")
#admin.register(HistoricalCustomModel)
class HistoricalCustomModelAdmin(admin.ModelAdmin):
...
I have a model named Lal in Django. I have successfully registered it in my admin.py file.
Now , what I want is if a particular url is hit then, I should be able to directly redirect using reverse_lazy to the page displaying the model contents that I had registered in admin.py file.
The url that gets generated when I access the model displaying template page using my admin-login directly is:
http://127.0.0.1:8000/admin/test1_app/lal
With the help of this line of code,
url(r'yahoo/$', RedirectView.as_view(url = reverse_lazy('admin:app_list',kwargs={'app_label': 'test1_app'})), name="yahoo")
I am able to successfully generate the following url:
http://127.0.0.1:8000/admin/test1_app/
What should I add more to generate the model's url i.e. this one:
http://127.0.0.1:8000/admin/test1_app/lal
The url name for a model's changelist is admin:{{ app_label }}_{{ model_name }}_changelist. So for your app/model you want:
reverse_lazy('admin:test1_app_lal_changelist')
See the docs on reversing admin urls for more info.
I'm new to django and I'm wondering why the database table I created with django models won't show up on the admin page.
Here's what I did in a bash window.
And my admin page.
It seems the Cards table has been created but I don't see the table on my admin page and I want to know why. Any advice will be much appreciated.
You have to enter your table name in admin.py file like this
from django.contrib import admin
admin.site.register(ModelName)
I'm running a photography app in Django. I have a Photograph model and a PhotoSet model with a ManyToManyField relationship to Photograph. I would like to create an admin action where I can select several Photograph objects in the admin list view, choose the "Create photo set from selected photos" action, and be taken to the admin:photography_photoset_add view with the photos field pre-populated with the photos I selected on the previous page. I'd then be able to enter the title, slug, and description as needed. Is this flow possible? I haven't been able to find it after quite a bit of searching and the only route I currently know of would be handling all of this myself with custom add views and storage of my selection in session state. Seems messy.
See this part of the docs:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/#actions-that-provide-intermediate-pages
And this answer:
https://stackoverflow.com/a/4924985/202168
from django.core.urlresolvers import reverse
def create_photoset_from_photos(modeladmin, request, queryset):
ids = ','.join([str(photo_id) for photo_id in queryset.values_list('id', flat=True)])
add_photoset_url = '{url}?photos={ids}'.format(url=reverse('admin:photography_photoset_add'), ids=ids)
return HttpResponseRedirect(add_photoset_url)
I have installed the django-registration app to my project. After a successful log in step, I am redirecting the user to localhost:8000/ - this is my default testing host and port. And I am displaying somewhere on the page, the username of the logged in user.
What I want to do now is that when I click the username some options like edit profile or change password will appear. My questions are the following:
Should I create another model (inside another new app) containing fields like profile photo, gender, birthday etc and add a foreign key to the User model from django.contrib.auth.models ? Or should I modify the model from django-registration to add some additional fields but which I do not ask for at registration phase and only update them later?
if I want my profile edit feature to be at /accounts/edit, which would be the best practice to do it? to edit the URLconf of my project and add a line like (r'^accounts/edit$',.....) just before (r'^accounts/', include('registration.backends.default.urls')), ?
I hope I made myself clear. I'm trying to figure out which would be the best approach before coding, as I am new to Django... Thanks
I find it's easier to decouple the profile table from the auth table. Just like you mentioned you can use a foreign key relationship to link that profile to the user. You can also apply a lambda inside of your profile table to automatically create a profile when a new user object is created.
Inside your template you can link to the profile page dynamically based on the current authenticated party by using
{% if request.user.is_authenticated %}
Update Profile
{% endif %}
user_profile being the name of your app which holds your user_profile table. That way when the request is made you use the regular expression for the current user id (similar to the polls example provided by django) to get the id number of the currently logged in user than inside the views you just query the database for that particular user.
views.py
def myView(request, user_id):
userProfile = UserProfile.objects.get(user.pk=user_id)
This is a high level example to give an idea of one way to accomplish it.