I'm working on an Django 3 app which allows users to add and remove data requests for review. The user will select the user and site from a list and depending on the availability of data will populate a date/time field to be able to select a start and end date range. I'm using the Tempus Dominus time date picker to set the available dates in the date picker and am hardcoding them for the time being. The end goal will be to query Elasticsearch for them but hardcoded is fine for now.
My question is; At the moment the form is static, meaning none of the selections change the content in other sections of the form. How can i make it dynamic so that the list of enableDates dates changes when the user selects a different user or site from the dropdown lists? Just to be clear, the list of users and sites will never change, it's just the enableDates that need to change.
I'd appreciate any help or advice.
At the moment the form for submitting a request, along with displaying any existing requests looks like:
This is my MODEL for storing data requests:
class DataRequest(models.Model):
USERS = [
('User1', 'User1'),
('User2', 'User2'),
('User3', 'User3'),
]
users = models.CharField(
choices=USERS,
max_length=32,
default='User1',
)
SITE = [
('Site1', 'Site1'),
('Site2', 'Site2'),
('Site3', 'Site3'),
]
site =models.CharField(
choices=SITE,
max_length=32,
default='Site1',
)
start = models.DateTimeField()
end = models.DateTimeField()
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
def get_request(self):
request = {
"id": self.id,
"users": self.users,
"site": self.site,
"start": self.start,
"end": self.end,
}
return request
This is the FORM:
class RequestForm(ModelForm):
start = forms.DateTimeField(
widget=DateTimePicker(
options={
},
attrs={
'append': 'fa fa-calendar',
'input_toggle': True,
'icon_toggle': True,
}
)
)
end = forms.DateTimeField(
widget=DateTimePicker(
options={
'useCurrent': False,
'enabledDates':["2021-02-20",], // hard coded dates go here
},
attrs={
'append': 'fa fa-calendar',
'input_toggle': True,
'icon_toggle': True,
}
)
)
class Meta:
model = DataRequest
fields = ['users','site', 'start','end']
def __init__(self, *args, **kwargs):
super(RequestForm, self).__init__(*args, **kwargs)
self.fields['users'].widget.attrs.update({'class' : 'form-control'})
self.fields['site'].widget.attrs.update({'class' : 'form-control'})
this is the VIEW for adding and removing requests, the list of existing data requests is sent over in the page context:
def delRequest(request, pk):
if request.method == 'POST':
request = DataRequest.objects.get(id=pk)
request.delete()
return redirect('dashboard')
def addRequest (request):
if request.method == 'POST':
form = RequestForm(request.POST)
if form.is_valid():
DataRequest.objects.create(
user=request.user,
users=form.cleaned_data['users'],
site=form.cleaned_data['site'],
start=form.cleaned_data['start'],
end=form.cleaned_data['end']
)
print("Added new Request")
return redirect('dashboard')