Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin custom descending and ascending field

I am using Django Admin to visualize and edit data of my Model. One of the model's field is date and unfortunately is a CharField but with a validator ( dd.mm.yyyy ). I am trying to figure out of a solution to sort those dates in the correct way when the admin press on the sort button that is default from django. In my experience with Java it was all about overwriting the compare function of the class.

data_de_montat = models.CharField(help_text="eg: 02.07.2020 or 12.9.2021", max_length=15, validators=[
    RegexValidator(r'^[0-9]{1,2}.[0-9]{1,2}.[0-9]{4}$',
                   message='It should be dd.ll.aaaa',
                   code='wrong format'
                   )])

the button

like image 900
learner123 Avatar asked Nov 24 '25 18:11

learner123


1 Answers

In django it is very easy. You only need to go in to admin.py make a class with you model name and than add filter on create date which automatically sort out all dates for you. Example:

Step 1: Go to admin.py of your app

Step 2:Add following code

class YourModelAdmin(admin.ModelAdmin):
list_filter=['data_de_montat']
like image 199
Ahmed Yasin Avatar answered Nov 26 '25 07:11

Ahmed Yasin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!