asaglimbeni / django-datetime-widget

Bootstrap django-datetime-widget is a simple and clean widget for DateField, Timefiled and DateTimeField in Django framework. It is based on Bootstrap datetime picker, supports both Bootstrap 3 and Bootstrap 2
Other
219 stars 125 forks source link

Does it work within the admin? #73

Open Luis-Palacios opened 9 years ago

Luis-Palacios commented 9 years ago

Hello and thank you for your awesome widget,

I am trying to use your widget in the admin for this i am trying via a form in the following way:

class MovieAdminForm(forms.ModelForm):
class Meta:
    model = Movie
    widgets = {
        'date': DateTimeWidget(attrs={'id':"id_date_0"}, usel10n = True, bootstrap_version=3)
        }  

@admin.register(Movie)
    class MovieAdmin(SummernoteModelAdmin):
    list_display = ('title', 'date','end_date', )
    search_fields = ('title',)
    form = MovieAdminForm
    fieldsets = (
    ('General Info',{
        'fields':('title','description', 'date', 'end_date', 'picture', 'room', ),
        }),
    )

this produce the following output: screenshot_3

at client side it all works great but once i try to save: screenshot_4

i believe this is because normally in the admin datetime field is repsented by two inputs one for time and one for date is there and easy workaround for this?

amlozano1 commented 9 years ago

Here is what I did for a quick work around, You can even use some options for each field (I wanted meridian and AM PM display for the time field):

from datetimewidget.widgets import TimeWidget, DateWidget
from django import forms

class AmPmSuitSplitDateTimeWidget(forms.SplitDateTimeWidget):
    def __init__(self, attrs=None, options=None, *args, **kwargs):
        widgets = [DateWidget(options=options), TimeWidget(options={'showMeridian': True,
                                                                'format': "HH:ii P"})]
        forms.MultiWidget.__init__(self, widgets, attrs)
symbiosdotwiki commented 9 years ago

how did you get that non-bootstrap theme on top?

echodelt commented 9 years ago

Hi,

I have encountered the same problem, here is the code I use now in my admin forms to avoid the "Enter a list of values" error :

from django import forms

from django.contrib import admin

from datetimewidget.widgets import DateTimeWidget

from .models import *

class TemperatureReportAdminForm(forms.ModelForm):

    class Meta:

        model = TemperatureReport
        fields = '__all__' 

    def __init__(self, *args, **kwargs):

        super(TemperatureReportAdminForm, self).__init__(*args, **kwargs)

        self.fields['measured_at'] = forms.DateTimeField()
        self.fields['measured_at'].widget = DateTimeWidget(usel10n = True, 
                                                           bootstrap_version=3)

class TemperatureReportAdmin(admin.ModelAdmin):

    class Media:

        js = ('https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js', 
              'bootstrap-3.3.5-dist/js/bootstrap.js',)
        css = {'all': ('bootstrap-3.3.5-dist/css/bootstrap.css',)}

    form = TemperatureReportAdminForm

admin.site.register(TemperatureReport, TemperatureReportAdmin)
javaghost commented 8 years ago

While using DateTimeInput (default for admin) it actually splits the date and time fields as id_fieldName_0 as date field and id_fieldName_1 as time field. When this DateTimeWidget is used, the post data contains only fieldName as single value, when django expects list of values. Hence in django admin, DateTimeWidget will always throw ValidationError to send values as list. (Tested in Django 1.8 with l10n false in settings)

symbiosdotwiki commented 8 years ago

I have this fixed for admin, I'll push the update in a second

Luis-Palacios commented 8 years ago

What i've been doing it is applying trough raw javascript overriding the change_form.html from the admin with:

$('#id_date_0').datetimepicker({ format: 'DD/MM/YYYY' }); $('#id_date_1').datetimepicker({ format: 'HH:mm:SS' });

symbiosdotwiki commented 8 years ago

https://github.com/asaglimbeni/django-datetime-widget/pull/74/commits/cb8b7cd6e1f587fc042079687cb59f9f6598507e

from pr https://github.com/asaglimbeni/django-datetime-widget/pull/74

jpdavy commented 7 years ago

Should this be closed?