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
Related
I want to be able to set a variable in the django admin panel, which will be passed to the template as template variable, similar to placeholder in cms. (For example when I save this variable in admin panel it will be rendered to the template)
Added.
Let's say I have a site page with text area, this text area I can replace by template variable in view with template tags {{ text }}. But I want to do it in the django admin panel, what i did was creating the model with text field, registering it in admin panel and replacing only one row in this table and geting this text variable in view that I want to pass in to the template as text_home = Text_home_page.objects.all()[0].text.
I think that creating whole table to modify only one template variable is not a good idea.
I found a similar question but i dont know how to implement it.
Your question is not very clear, are you referring to a default value, if you are you can easily create a default value argument to your model field, or you can create a new field to store a default value, then you can set a condition to use the default value if the actual value is empty
this might not be a good practice but I hope you can edit your question to make it clearer, and add a code sample of what you might have tried to do
I'd like to provide filter functionality in django admin list, but the list of available items to filter might be huge. I'd like to have select box, or better input field where the items list would decrease while typing. What would be the best approach?
I have been able to control the rendering of a custom list filter easily in my project. That should be enough to achieve your needed level of customization.
First, you have to create a custom list filter, let's call it CustomListFilter by subclassing django.contrib.admin.SimpleListFilter as explained in the Django documentation site. Then, you change the template used by your custom filter by setting the template class variable (CustomListFilter.template) to a template path with your custom rendering (the original template is in django/contrib/admin/templates/admin/filter.html).
I want to create a dynamic admin site, that based on if the field is blank or not will show that field. So I have a model that has a set number of fields, but for each individual entry will not contain all of the fields in my model and I want to exclude based on if that field is blank.
I have a unique bridge identifier, that correlates to each bridge, and then all of the various different variables that describe the bridge.
I have it set up now that the user will go to a url with the unique bridgekey and then this will create an entry of that bridge. So (as i am testing on my local machine) it would be like localhost/home/brkey and that code in my views.py that corresponds to that url is
However, not every bridge is the same and I have a lot more variables that I would like to include in my model but for now I am just testing on two : prestressed_concrete_deck and reinforced_concrete_coated_bars. What I want is to dynamically create the admin site to not display the prestressed_concrete_deck variable if that field is blank. So instead of displaying all of the variables on the admin site, I want to only display those variables if that bridge has that part, and to not display anything if the field is blank.
Another possible solution to the problem would be to get that unique identifier over to my admins.py. I cant figure out either how to get that individual key over as then I could query in the admins.py. If i knew how to access the bridgekey, I could just query in my admins.py dynamically. So how would I access the brkey for that entry in my admins.py (Something like BridgeModel.brkey ?)
I have tried several different things in my admin.py and have tried the comments suggestion of overwriting the get_fields() method in my admin class, but I am probably syntactically wrong and I am kind of confused what the object it takes exactly is. Is that the actual entry? Or is that the individual field?
Just override the get_fields method in your ModelAdmin class.
You can check the obj is passed as function argument so you can check which fields are empty. The function needs to return a tuple so, you would check if field1 is None and then return (field1, field2, field3) or (field2, field3) depending on the value of field1.
I was using Django 1.6 which did not support overriding the get_fields method. Updated to 1.7 and this method worked perfectly.
I'm a newbie and right now I'm using Django forms to show some fields. One of these fields is a ModelChoiceField this show correctly model data, but I donĀ“t know how I can fill a CharField later of select a option in the ModelChoiceField.
How I can make send the value of option selected for obtain the data that i need for later show this in a CharField, this is possible make directly from a djangoForm or do I need a new view to return the value?
There are two ways to achieve what you want I think:
Provide an initial value for your form value.
Bind your form to some data.
See this related stack overflow to provide initial values.
See Django's documentation on forms for binding data. There are two kinds of form states: bound and unbound. To create a bound form, you just need to create an instance of your form with some data: MyForm({'data-key1': 'data-value1', ...}).
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.