| 1 |
from django.contrib import admin |
| 2 |
from .models import Restaurant, Rating |
| 3 |
|
| 4 |
|
| 5 |
@admin.register(Restaurant) |
| 6 |
class RestaurantAdmin(admin.ModelAdmin): |
| 7 |
list_display = ['name', 'address', 'average_rating', 'total_ratings', 'created_at'] |
| 8 |
list_filter = ['created_at', 'average_rating'] |
| 9 |
search_fields = ['name', 'address', 'place_id'] |
| 10 |
readonly_fields = ['average_rating', 'total_ratings', 'created_at'] |
| 11 |
|
| 12 |
fieldsets = ( |
| 13 |
('Basic Information', { |
| 14 |
'fields': ('place_id', 'name', 'address') |
| 15 |
}), |
| 16 |
('Location', { |
| 17 |
'fields': ('latitude', 'longitude') |
| 18 |
}), |
| 19 |
('Ratings', { |
| 20 |
'fields': ('average_rating', 'total_ratings'), |
| 21 |
'description': 'These fields are automatically calculated' |
| 22 |
}), |
| 23 |
('Metadata', { |
| 24 |
'fields': ('created_at',) |
| 25 |
}) |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
@admin.register(Rating) |
| 30 |
class RatingAdmin(admin.ModelAdmin): |
| 31 |
list_display = ['restaurant', 'rating', 'review_preview', 'created_at'] |
| 32 |
list_filter = ['rating', 'created_at'] |
| 33 |
search_fields = ['restaurant__name', 'review'] |
| 34 |
raw_id_fields = ['restaurant'] |
| 35 |
|
| 36 |
def review_preview(self, obj): |
| 37 |
return obj.review[:50] + '...' if len(obj.review) > 50 else obj.review |
| 38 |
review_preview.short_description = 'Review' |