◾️ We can add search bar with search_fields in admin panel(ref site)
search_fields = (“city”, “address”, “host__username”)
-> we can search city or address or username of host field with ignore case
@admin.register(models.Room)
class RoomAdmin(admin.ModelAdmin):
""" Room Admin Definition """
list_display = (
"name",
"country",
"city",
"price",
"address",
"guests",
"beds",
"bedrooms",
"baths",
"check_in",
"check_out",
"instant_book",
"host",
)
list_filter = ("instant_book", "city", "country", "host")
search_fields = ("city", "address", "host__username")
- search_fields prefix
Some (older) shortcuts for specifying a field lookup are also available. You can prefix a field in search_fields with the following characters and it’s equivalent to adding __<lookup> to the field:
Prefix Lookup
^ startswith
= iexact
@ search
None icontains
◾️ filter_horizontal can select items for many to many field selection
@admin.register(models.Room)
class RoomAdmin(admin.ModelAdmin):
""" Room Admin Definition """
list_display = (
"name",
......
"instant_book",
"host",
)
list_filter = (
"instant_book",
"host__superhost",
"room_type",
"amenities",
"facilities",
"house_rules",
"city",
"country",
)
search_fields = ("city", "address", "^host__username")
filter_horizontal = ("amenities", "facilities", "house_rules")
class Room(core_models.AbstractTimeStampModel):
""" Room Model Definition """
name = models.CharField(max_length=140)
.....
room_type = models.ForeignKey("RoomType", on_delete=models.SET_NULL, null=True)
amenities = models.ManyToManyField("Amenity", blank=True)
facilities = models.ManyToManyField("Facility", blank=True)
house_rules = models.ManyToManyField("HouseRule", blank=True)
- we can select items(amenities/facilities/house_rules) from admin panel
◾️ ordering
@admin.register(models.Room)
class RoomAdmin(admin.ModelAdmin):
""" Room Admin Definition """
list_display = (
"name",
......
"instant_book",
"host",
)
ordering = ("name", "price", "bedrooms")
◾️ list_display : many to many relation customized field
list_display = (
"name",
"country",
...
"instant_book",
"host",
"count_amenities",
)
def count_amenities(self, obj):
return obj.amenities.count()