To create advanced configuration for Django admin with fieldsets, search, and filter, you can follow these steps:
1. Define your Django model: Start by defining your Django model in your app's `models.py` file. For example, let's say you have a model called `Product` with several fields.
```python
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
price = models.DecimalField(max_digits=8, decimal_places=2)
category = models.ForeignKey('Category', on_delete=models.CASCADE)
```
2. Register the model in the admin site: In your app's `admin.py` file, import the model and register it with the admin site.
```python
from django.contrib import admin
from .models import Product
admin.site.register(Product)
```
3. Customize the admin interface: To configure advanced options for the admin interface, you can override the default `ModelAdmin` class. Create a new class that inherits from `admin.ModelAdmin` and override the desired settings.
```python
from django.contrib import admin
from .models import Product
class ProductAdmin(admin.ModelAdmin):
search_fields = ['name'] # Specify the fields to search in the admin interface
list_filter = ['category'] # Add filters for the specified fields
fieldsets = (
('General Information', {
'fields': ('name', 'description', 'price')
}),
('Category', {
'fields': ('category',)
}),
) # Group fields into sections (fieldsets) for better organization
admin.site.register(Product, ProductAdmin)
Collapsed fieldsets
fieldsets = [ ( None, { "fields": ["url", "title", "content", "sites"], }, ), ( "Advanced options", { "classes": ["collapse"], "fields": ["registration_required", "template_name"], }, ), ]
```
In the above example, we've added search functionality for the `name` field using the `search_fields` attribute. We've also added a filter option for the `category` field using the `list_filter` attribute. The `fieldsets` attribute is used to group fields into sections for better organization in the admin interface.
With these customizations, you should see the advanced configuration in the Django admin interface for the `Product` model, including the search field, filter options, and fieldset organization.
Note: Make sure to run the necessary migrations (`python manage.py makemigrations` and `python manage.py migrate`) after making changes to the models or admin configuration.
Readonly_fields
By default, the Django admin interface allows all fields to be editable. However, you can specify certain fields as read-only by using the `readonly_fields` attribute in your `ModelAdmin` class. This attribute should be a list or tuple containing the fields that you want to display as non-editable.
The fields specified in `readonly_fields` will be displayed as-is in the admin interface and cannot be modified. Additionally, they are excluded from the form used for creating and editing objects. It's important to note that if you have explicitly defined the `fields` or `fieldsets` attributes in your `ModelAdmin` class, the read-only fields must be present in order to be shown; otherwise, they will be ignored.
If you use `readonly_fields` without explicitly defining the field ordering through `ModelAdmin.fields` or `ModelAdmin.fieldsets`, the read-only fields will be added at the end of the form after all the editable fields.
A read-only field can display data not only from a model's field but also from a method of the model or a method of the `ModelAdmin` class itself. This functionality is similar to how `ModelAdmin.list_display` works. This allows you to use the admin interface to provide feedback on the status of the objects being edited. For example, you can display computed values or call methods to generate custom display outputs for read-only fields.
readonly_fields = ["address_report"]
class OfferAdmin(admin.ModelAdmin):
save_as = True
ordering = ['-modified', '-created']
search_fields = ('vehicle_merk__name', 'vehicle_model__name', 'name', 'owner_description', 'note' )
list_filter = ('offer_status', 'offer_type', 'created', 'vehicle_merk')
list_display = ('__str__', 'created', 'owner_description')
raw_id_fields = ('stock_vehicle',)
readonly_fields = ('automatic_purchase_price', 'unique_key', 'source',)
fieldsets = [
(
"Contactpersoon",
{
"fields": ["name", "email", "phone_number", "source"],
},
),
(
"Vehicle information",
{
"fields": ["offer_type", "owner_description", "vehicle_merk", "vehicle_model", "mileage", "first_registration", "fuel_type", "gearbox", "owner_laste_price", "estimate_purchase_price", "automatic_purchase_price", "offer_status", "note", "created", "offer_type", "stock_vehicle"],
},
),
(
"Advanced Vehicle information",
{
"fields": ["paint", "colour", "emission_class", "engine_size", "doors", "seats", "carrosserietype", "power_kw", "power_hp", ],
},
),
]
Comments
Post a Comment