Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does it work within the admin? #73

Open
Luis-Palacios opened this issue May 8, 2015 · 8 comments
Open

Does it work within the admin? #73

Luis-Palacios opened this issue May 8, 2015 · 8 comments

Comments

@Luis-Palacios
Copy link

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
Copy link

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
Copy link

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

@echodelt
Copy link

echodelt commented Jul 1, 2015

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
Copy link

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
Copy link

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

@Luis-Palacios
Copy link
Author

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
Copy link

cb8b7cd

from pr #74

@jpdavy
Copy link

jpdavy commented Sep 11, 2017

Should this be closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants