I am trying to do this cool stuff in django admin
I have a model which has a ImageField. What I want is that the my other coworkers are able to decide if they want to upload image or just paste the image link.
like
<radio>: upload <radio>: link
and depending on choice, respective field (which is model's imageField) will show up for under these radio buttons.
how can I achieve this in django admin?
You'll need to add the additional field to your admin form class, then add some JavaScript to show or hide the appropriate field on page load, and also add an event handler to show the field to upload or paste in the link.
Both fields would need to allow blank=True and then you'd need to add a clean() method to make sure one of the fields is populated and then set the value appropriately. You might be better off using two separate fields.
You can leverage the Media inner class to easily add the JavaScript to the page without having to alter the change_form.html template for that app. Check out: https://docs.djangoproject.com/en/1.7/topics/forms/media/ for examples of how to add custom CSS and JavaScript to forms.
Related
I have a table with a foreign key. My problem is that ther's a lot of registers, so I need do that:
But all I've found was for the Admin Panel. Any idea for a custom form without admin?
Raw id, isn't needed any more, since there's autocomplete in the admin. This is also the solution for a model in the frontend. However, you should not bake it yourself, but just use Django Autocomplete Light. An excerpt from the tutorial:
Autocompletes are based on 3 moving parts:
widget compatible with the model field, does the initial rendering,
javascript widget initialization code, to trigger the autocomplete,
and a view used by the widget script to get results from.
I have a model Order and model Invoice. The Order has
invoice = models.OneToOneField('Invoice', related_name='order', on_delete=models.CASCADE, blank=True, null=True)
Invoice object is created right after order object is created an assigned to it. Admin has to edit the invoice (price field) before customer pays.
The problem is that Django-admin allows admin to change this field too (bottom of the image), which I can't risk but I want to let the pencil icon (change attributes of the invoice).
Is it possible to do that? When I add invoice to readonly_fields in OrderAdmin, Admin can't edit those attributes like invoice.price etc.
EDIT:
So I want admin to be able to edit attributes of the invoice. Not add nor choose from dropdown.
One option would be to provide a custom template for this view. The docs say that you can specify a path to a custom template using ModelAdmin.change_form_template.
Here is a section of the docs that talk about how to override a template: https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#overriding-vs-replacing-an-admin-template
Though this is not the optimal solution, you could probably use Javascript to hide/disable the parts you don't want.
Finally, you may want to consider your usage of the Django admin:
The admin’s recommended use is limited to an organization’s internal
management tool. It’s not intended for building your entire front end
around.
The admin has many hooks for customization, but beware of trying to
use those hooks exclusively. If you need to provide a more
process-centric interface that abstracts away the implementation
details of database tables and fields, then it’s probably time to
write your own views.
def has_add_permission(self, request):
return False
Django admin: How to display a field that is marked as editable=False' in the model?
I have one admin page. Where I want to add some link such that on click on that link pop up should be visible where we can set some value and alert something. I Don't want to post something in back end. So How we can achieve this without downloading change_list.html and change_form.html from default django site packages.?
You can override admin templates. As the docs state, you override the block you are interested modifying the contents of that block, by placing it in a file in a certain directory, and naming it after what type of page you want to change.
It's all explained in the docs.
I'd like to add a derived field to a default ModelAdmin.fieldsets like I would by specifying a method and adding it to the ModelAdmin.list_display property, but there doesn't seem to be an easy way to do that (if there is ANY way to do that).
The default Django Admin list view seems to have a lot more options than the change form view does.
Let's say I have two fields for a location: latitude and longitude, and instead of displaying them on a change form I want to display a Google Maps Static Map image instead - I already have a method that will return the src url for the image - I just need a way to add that image to the model change form instead of showing those two fields.
If you write a method and add it to ModelAdmin.readonly_fields, it will appear on the change view.
customize admin view http://docs.djangoproject.com/en/dev/ref/contrib/admin/#ref-contrib-admin
This question already has answers here:
Django Admin - Disable the 'Add' action for a specific model
(5 answers)
Closed 10 years ago.
Is there a way to remove the "Add" functionality on the Django admin site? For certain entities, I only want the Django admin to be able to view them or change existing ones, but not add new ones.
See: Django Admin - Disable the 'Add' action for a specific model for true solution.
Sure, you can customize admin VERY granularly by following the instructions here -- I believe that what you want can be obtained in part by overriding ModelAdmin.save_model(self, request, obj, form, change) in your own ModelAdmin subclass, to ensure nothing happens on the store when change is false (i.e. an attempt to add rather than change), and in part by overriding ModelAdmin.add_view(self, request, form_url='', extra_context=None) to display an "add view" that makes it very clear to the admin that they're NOT going to be allowed to add object through this route. I haven't actually done the specific admin customization you require, but I've done others and they do seem to work pretty smoothly!
You can customize the permission for each user group from within the admin interface: try going to /admin/auth/group and it should be straightforward from there.
This won't be as granular as the solution offered by the earlier answer, but it will take care of most of your needs without needing to customize the admin.
If you change the permissions to restrict access then you'll still get the plus sign by a FK/MtM field. Clicking that will open a popup window with 'Permission Denied' in it.
You can actually completely remove the plus sign by not simply not registering the model with the admin.
I have a situation where I have predefined categories that I want users to be able to select more than one of. The best way to do this is with a models.ManyToMany field. You can register the model with the admin, enter the data as required and then remove the registration.
An easy effective way is to set max_num=0 for that particular inline.
Satya's suggestion of setting max_num=0 works perfectly.
Per the Django docs on the ModelForm class:
For users with JavaScript-enabled browsers, an "Add another" link is provided to enable any number of additional inlines to be added in addition to those provided as a result of the extra argument.
The dynamic link will not appear if the number of currently displayed forms exceeds max_num, or if the user does not have JavaScript enabled.
and
As with regular formsets, you can use the max_num and extra parameters to modelformset_factory to limit the number of extra forms displayed.
max_num does not prevent existing objects from being displayed