Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django SplitDateTime widget throws 'list' object has no attribute 'strip'

I am trying to create event with start date-time and end date-time with django. In the forms I've tried to seperate the date and time inputs with AdminSPlitDateTime widget (I'd like to have some JS for the input.) When I tried to create an event django gives me "'list' object has no attribute 'strip'" error. Also happens with SplitDateTimeWidget.

When I removed the widget or use DateTimeInput widget (no split) everything works as expected. But User can confuse to enter the proper format for date time. SO I want to user AdminSplitDateTime.

It must be something with spliting the date and time with widgets but I couldn't figure a way to solve this.

Using Python 3.7 django 2.2.7 pipenv 2018.11.26

my Model is

class Event(models.Model):
    event_start = models.DateTimeField()
    event_end = models.DateTimeField()
    notes = models.CharField(max_length=140, null=True, blank=True)
    hasta = models.ForeignKey(Hasta, on_delete=models.CASCADE)
    doktor = models.ForeignKey(User, on_delete=models.CASCADE)



    def __str__(self):
        return f'{self.event_start} -- {self.event_end}'

    class Meta:
        ordering = ['event_start'] 

my Form class is:

class EventCreateForm(ModelForm):
    event_start = forms.DateTimeField(widget= admin_widgets.AdminSplitDateTime())
    event_end = forms.DateTimeField(widget=admin_widgets.AdminSplitDateTime())

    def __init__(self, *args, **kwargs):
        """this whole thing is for filtering the hasta objects. User can only create an event with his patients."""
        self.user = kwargs.pop('user')
        super().__init__(*args, **kwargs)
        self.fields['hasta'].queryset = Hasta.objects.filter(doktor_id=self.user.id)

    class Meta:
        model = Event
        fields = [
            'event_start',
            'event_end',
            'notes',
            'hasta',
        ]
        labels = {
            'event_start': _('Randevu Başlangıç Tarihi'),
            'event_end': _('Randevu Bitiş Tarihi'),
            'notes': _('Notlar')

        } 

And my View class is:

class EventCreateView(LoginRequiredMixin, CreateView):
    model = Event
    template_name = 'create_event.html'
    form_class = EventCreateForm
    success_url = '/randevu'

    def get_form_kwargs(self):
        """This adds the user object to the kwargs and it will be used in create event form to filter
        the patients of the current user"""
        kwargs = super(EventCreateView, self).get_form_kwargs()
        kwargs.update({'user': self.request.user})
        return kwargs

    def form_valid(self, form):
        """This adds the current user id to the created event"""
        form.instance.doktor = self.request.user
        form.save()
        messages.success(self.request, f"randevu Kaydedildi!")
        return super().form_valid(form)

I am getting the below error;

AttributeError: 'list' object has no attribute 'strip'
[19/Nov/2019 13:34:11] "POST /randevu/yeni_randevu/ HTTP/1.1" 500 117105
Internal Server Error: /randevu/yeni_randevu/
Traceback (most recent call last):
  File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\contrib\auth\mixins.py", line 52, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\views\generic\edit.py", line 172, in post
    return super().post(request, *args, **kwargs)
  File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\views\generic\edit.py", line 141, in post
    if form.is_valid():
  File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\forms\forms.py", line 185, in is_valid
    return self.is_bound and not self.errors
  File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\forms\forms.py", line 180, in errors
    self.full_clean()
  File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\forms\forms.py", line 381, in full_clean
    self._clean_fields()
  File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\forms\forms.py", line 399, in _clean_fields
    value = field.clean(value)
  File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\forms\fields.py", line 148, in clean
    value = self.to_python(value)
  File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\forms\fields.py", line 462, in to_python
    result = super().to_python(value)
  File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\forms\fields.py", line 379, in to_python
    value = value.strip()
AttributeError: 'list' object has no attribute 'strip'

like image 314
Calavera Avatar asked Oct 16 '22 09:10

Calavera


1 Answers

You have to use SplitDateTimeField with AdminSplitDateTime.

like image 103
Daniel Hepper Avatar answered Oct 20 '22 10:10

Daniel Hepper