0
votes

I'm working on a project in which I need to use a few DateTime fields, I have defined DatetimeField in my model and then in the template, I'm using the https://tempusdominus.github.io/bootstrap-4/ plugin but when I submit the form there are two issues comes on:

1): Django says Enter a valid date/time for all DateTime Fields 2): Select a valid choice. ['corn_oil'] is not one of the available choices.

Here's what I have tried:

From models.py:

choices = (
    ('CO2 SCRUBBER', 'CO2 SCRUBBER'),
    ('corn_oil', 'CORN OIL'),
    ('DRYERS', 'DRYERS'),
    ('ENVIRONMENTAL', 'ENVIRONMENTAL'),
    ('UTILITIES', 'UTILITIES'),
    ('LAB', 'LAB'),
    ('SIEVES', 'SIEVES'),
    ('GRAINS & MILLING', 'GRAINS & MILLING'),
    ('SEPARATION', 'SEPARATION'),
    ('AIR & GAS', 'AIR & GAS'),
    ('COOK', 'COOK'),
    ('EVAPORATION', 'EVAPORATION'),
    ('WATER', 'WATER'),
    ('STORAGE', 'STORAGE'),
    ('BOILER', 'BOILER'),
    ('FERMENTATION', 'FERMENTATION'),
    ('DISTILLATION', 'DISTILLATION'),
    ('BUILDING AND FACILITIES', 'BUILDING AND FACILITIES'),
    ('CHEMICAL', 'CHEMICAL'),
)


class ExperimentModel(models.Model):
    user = models.ForeignKey(User, related_name='experiments',
                             on_delete=models.CASCADE)
    name = models.CharField(max_length=255)
    start_date = models.DateTimeField()
    change_date = models.DateTimeField()
    end_date = models.DateTimeField()
    assets = models.CharField(max_length=450, choices=choices)
    goals = models.CharField(max_length=255, blank=True)
    comments = models.TextField(max_length=1000)
    created_at = models.DateTimeField(auto_now=True)

From forms.py:

class ExperimentForm(forms.ModelForm):
    choices = (
        ('CO2 SCRUBBER', 'CO2 SCRUBBER'),
        ('corn_oil', 'CORN OIL'),
        ('DRYERS', 'DRYERS'),
        ('ENVIRONMENTAL', 'ENVIRONMENTAL'),
        ('UTILITIES', 'UTILITIES'),
        ('LAB', 'LAB'),
        ('SIEVES', 'SIEVES'),
        ('GRAINS & MILLING', 'GRAINS & MILLING'),
        ('SEPARATION', 'SEPARATION'),
        ('AIR & GAS', 'AIR & GAS'),
        ('COOK', 'COOK'),
        ('EVAPORATION', 'EVAPORATION'),
        ('WATER', 'WATER'),
        ('STORAGE', 'STORAGE'),
        ('BOILER', 'BOILER'),
        ('FERMENTATION', 'FERMENTATION'),
        ('DISTILLATION', 'DISTILLATION'),
        ('BUILDING AND FACILITIES', 'BUILDING AND FACILITIES'),
        ('CHEMICAL', 'CHEMICAL'),
    )
    assets = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
                                       choices=choices)
    # assets = ModelMultipleChoiceField(queryset=Thing.objects.all(), widget=Select2MultipleWidget)

    class Meta:
        model = ExperimentModel
        fields = ('user', 'name', 'start_date', 'change_date', 'end_date', 'assets',
                  'goals', 'comments')

From views.py: def post(self, request, *args, **kwargs): post_data = request.POST.copy() post_data.update({'user': request.user.pk}) form = ExperimentForm(post_data) if request.method == 'POST': print('req submitted') if form.is_valid(): print('form valid') form.save(commit=False) form.user = request.user form.save() return HttpResponseRedirect('all-experiments') return render_to_response('experiment/new_experiment.html', {'form': form})

And here how I'm displaying the DateTime field and choices field in my template: From new-experiment.html:

<div class="form-group">
                <input type="text" name="name" id="name" class="form-control input-lg"
                       placeholder="experiment name" tabindex="3" required>
            </div>
            <div class="form-group">
                <label for="datetimepicker1"> Start Date </label>
                <div class="input-group date" id="datetimepicker1" data-target-input="nearest">
                    <input type="text" name="start_date" class="form-control datetimepicker-input"
                           data-target="#datetimepicker1"/>
                    <div class="input-group-append" data-target="#datetimepicker1" data-toggle="datetimepicker">
                        <div class="input-group-text"><i class="fa fa-calendar"></i></div>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label for="datetimepicker2"> Change Date </label>
                <div class="input-group date" id="datetimepicker2" data-target-input="nearest">
                    <input type="text" name="change_date" class="form-control datetimepicker-input"
                           data-target="#datetimepicker2"/>
                    <div class="input-group-append" data-target="#datetimepicker2" data-toggle="datetimepicker">
                        <div class="input-group-text"><i class="fa fa-calendar"></i></div>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label for="datetimepicker3"> End Date </label>
                <div class="input-group date" id="datetimepicker3" data-target-input="nearest">
                    <input type="text" name="end_date" class="form-control datetimepicker-input"
                           data-target="#datetimepicker3" placeholder="dd/mm/yy"/>
                    <div class="input-group-append" data-target="#datetimepicker3" data-toggle="datetimepicker">
                        <div class="input-group-text"><i class="fa fa-calendar"></i></div>
                    </div>
                </div>
            </div>

            <script type="text/javascript">
                $(function () {
                    $('#datetimepicker1').datetimepicker();
                    $('#datetimepicker2').datetimepicker();
                    $('#datetimepicker3').datetimepicker();
                });
            </script>
            <div class="form-group">
                <label for="assets"> Assets </label>
                                <input type="text" name="assets" id="assets" class="form-control input-lg"
                                       placeholder="Assets" tabindex="3" required>
                <select name="assets" class="form-control select_field_class" id="assets">
                    <option value="CO2 SCRUBBER">CO2 SCRUBBER</option>
                    <option value="corn_oil">CORN OIL</option>
                    <option value="DRYERS">DRYERS</option>
                    <option value="ENVIRONMENTAL">ENVIRONMENTAL</option>
                    <option value="UTILITIES">UTILITIES</option>
                    <option value="LAB">LAB</option>
                    <option value="SIEVES">SIEVES</option>
                    <option value="GRAINS & MILLING">GRAINS & MILLING</option>
                    <option value="SEPARATION">SEPARATION</option>
                    <option value="AIR & GAS">AIR & GAS</option>
                    <option value="COOK">COOK</option>
                    <option value="EVAPORATION">EVAPORATION</option>
                    <option value="WATER">WATER</option>
                    <option value="STORAGE">STORAGE</option>
                    <option value="BOILER">BOILER</option>
                    <option value="FERMENTATION">FERMENTATION</option>
                    <option value="BUILDING AND FACILITIES">BUILDING AND FACILITIES</option>
                    <option value="CHEMICAL">CHEMICAL</option>
                </select>
            </div>
            <div class="form-group">
                <label for="assets"> Goals </label>
                <input type="text" name="goals" id="goals" class="form-control input-lg"
                       placeholder="Goals" tabindex="3" required>
            </div>
            <div class="form-group">
                <label for="comments"> Comments </label>
                <textarea name="comments" id="comments" class="form-control input-lg"
                          rows="5" required>
                    </textarea>
            </div>

I want to use multi-select for assets also, but not achieved yet.

Helm me, please!

Thanks in advance!

1

1 Answers

0
votes
import datetime

class ExperimentModel(models.Model):
    start_date = models.DateTimeField(auto_now_add=True, blank=True)
    change_date = models.DateTimeField(blank=True)
    end_date = models.DateTimeField(blank=True)