Django Admin Filtering Drop Down Menu by Date

471 views
Skip to first unread message

mab.mo...@gmail.com

unread,
Oct 9, 2018, 3:52:22 PM10/9/18
to Django users
Hello, 

I am trying to develop an employee schedule based on upcoming events and I only need to make additions or updates in Django Admin. To add a new event I would like to pick from a drop down list of only the events that are in the future or on todays date not the entire table list of events that may go back several years. I would do normally do this in a view like this ....

events  = WineryEvents.objects.filter(publish='Y').filter(event_date__gte=datetime.now()).order_by('event_date')

However, I am unsure of how to do this in my main/admin.py. 

The drop down list would ideally be ordered by date and only for future or current events and formatted like this...

(EVENT DATE MM/DD/YY EVENT TITLE)  

Could someone help me out with the code to put in the main/admin.py to accomplish this? Model and admin.py info below...

main/models.py

class EmployeeSchedule(models.Model):
    user = models.ForeignKey(CustomUser, on_delete=models.PROTECT, blank=True,  null=True)
    event = models.ForeignKey(WineryEvents, on_delete=models.PROTECT)
    role = models.CharField(choices=ROLE, max_length=40)
    start_time = models.TimeField(auto_now_add=False)
    comment = models.TextField(blank=True)

    def __str__(self):
       return self.role

restaurant/models.py

class WineryEvents(models.Model):
   event_date = models.DateField(auto_now_add=False)
   event_time = models.TimeField(auto_now_add=False)
   event_type = models.CharField(max_length=20, choices=EVENT_TYPE)
   seating = models.PositiveIntegerField(default=0)
   title = models.CharField(max_length=200)
   description = models.TextField()
   menu = models.TextField()
   price = models.DecimalField(max_digits=6, decimal_places=2)
   publish = models.CharField(max_length=1, choices=PUBLISH_CHOICE)

   def __str__(self):
      return self.title

And the following main/admin.py

class EmployeeScheduleAdmin(admin.ModelAdmin):

   def user_info(self, obj):
     return obj.user.get_full_name()

   list_display = ['event','user_info','role', 'start_time']
   list_filter = ['event','user']
   ordering = ['-event']

admin.site.register(models.EmployeeSchedule, EmployeeScheduleAdmin)

Derek

unread,
Oct 12, 2018, 2:26:43 AM10/12/18
to Django users
Your question does not make sense (as written): "To add a new event I would like to pick from a drop down list of only the events that are in the future" ..

Why should adding a new event depend on you selecting another event?  Are the events related or dependant in some way; that is not clear?

If you want to add a new EmployeeSchedule, and select a "future event"  there, then its best to set its ForeignKey to a model proxy of WineryEvents whose object manager filters out past events.

Nelson Varela

unread,
Oct 12, 2018, 10:57:31 AM10/12/18
to Django users
You could make a custom form for your admin which is a model form which points to EmployeeSchedule. And in the form its init you can change the queryset of the events field:

self.fields['events'].queryset = WineryEvents.objects.filter(publish='Y').filter(event_date__gte=datetime.now()).order_by('event_date')

 

mab.mo...@gmail.com

unread,
Oct 15, 2018, 12:28:47 PM10/15/18
to Django users
Thank you Nelson. I will try this option.

mab.mo...@gmail.com

unread,
Oct 15, 2018, 5:29:17 PM10/15/18
to Django users
Hello Nelson, 

I may need you to walk me through this more. I am relatively new to Django. I modified my admin.py and added from django import forms and added the following code in the admin.py file. It's still giving me an error ...

"type object 'EmployeeSchedule' has no attribute 'events'"

I'm sure that's not the only issue and I'm doing something else wrong. 

Just to make sure I'm clear on what I need this to do, I would like the drop down list to appear as below when adding a new employee shift/schedule date or editing an existing one... What should be submitted to the database table/model is the event id but I would like human readable text to choose from with only future or current events. 

(EVENT DATE MM/DD/YY EVENT TITLE)  

## Employee Schedule

class EmployeeScheduleForm(forms.Form):
#    pass
#   self.fields['events'].queryset = WineryEvents.objects.filter(publish='Y').filter(event_date__gte=datetime.now()).order_by('event_date')
    events  = WineryEvents.objects.filter(publish='Y').filter(event_date__gte=datetime.now()).order_by('event_date')

class EmployeeScheduleAdmin(admin.ModelAdmin):

   def user_info(self, obj):
     return obj.user.get_full_name()

   list_display = ['events','user_info','role', 'start_time']
#   list_filter = ['event','user']
   ordering = ['-event']

admin.site.register(models.EmployeeSchedule, EmployeeScheduleAdmin)

-----------------------------------------------------------------------

On Friday, October 12, 2018 at 9:57:31 AM UTC-5, Nelson Varela wrote:
Reply all
Reply to author
Forward
0 new messages