我正在尝试使用事件模型在我的网站中使用日历功能。
class Event(models.Model):
limit = models.Q(app_label='crm', model='Household', id=10) | \
models.Q(app_label='crm', model='Lead', id=13) | \
models.Q(app_label='crm', model='Opportunity', id=14) | \
models.Q(app_label='crm', model='Case', id=11)
name = models.CharField(
pgettext_lazy("Name of the Event", "Event"),
max_length=64)
我的html上有。事件显示正常。 现在,添加或编辑事件。我有这个。
$(document).ready(function() {
$('#calendar').fullCalendar({
defaultDate: '{{today|date:'%Y-%m-%d'}}',
editable: true,
header: {
left: 'prev,next today prevYear',
center: 'title',
right: 'nextYear month,agendaWeek,agendaDay'
},
navLinks: true, // can click day/week names to navigate views
eventLimit: true, // allow "more" link when too many events
events: [
{% for i in events %}
{
title: "{{ i.name}}",
start: '{{ i.start_date|date:"Y-m-d" }}',
end: '{{ i.close_date|date:"Y-m-d" }}',
},
{% endfor %}
],
firstDay:1,
slotDuration:'00:15:00',
scrollTime:'07:00:00',
selectable: true,
selectHelper: true,
ignoreTimezone: false,
lazyFetching:true,
select: function(start, end) {
eventType="";
$("#eventDialogLabel").html("New Appointment");
$("#eventID").val('');
$('#eventDialog').modal('show');
startDate=start;
$("#deleteButton").hide();
},
eventClick: function(calEvent, jsEvent, view) {
eventType="";
$("#eventDialogLabel").html("Update Appointment");
$("#eventID").val(calEvent._id);
alert(calEvent._id);
$("#modal-body").html(calEvent.title);
$("#deleteButton").show();
$('#eventDialog').modal('show');
startDate=calEvent.start;
endDate=calEvent.end;
},
eventDragStart:function( event, jsEvent, ui, view ) {
eventType="eventDragStart";
startDate=event.start
},
eventDrop: function(event, delta, revertFunc) {
endDate=event.start;
EventDropOrResize(calEvent);
},
eventResize: function(event, delta, revertFunc) {
EventDropOrResize(calEvent);
},
});
});
$("#deleteButton").on('click',function(){
doPOST('DELETE');
return false;
});
function EventDropOrResize(calEvent)
{
$("#eventID").val(calEvent._id);
alert(calEvent._id);
endDate=event.start;
startDate=event.end;
appointmentType=event.allDay
doPOST("PUT");
}
function doPOST(methodType)
{
$("#eventDialog").modal('hide');alert(calEvent);
url="{% url 'riskengine:change_event' %}"
if(calEvent !='')
{alert(calEvent);
url+="?id="+$("#eventID").val();
}
var event={
name_id: $("#id_title").val(),
end:new Date(endDate),
start:new Date(startDate),
type:$('#id_type').val(),
allDay:appointmentType
}
$.ajax({
type:methodType,
url:url,
datatype:'JSON',
data:JSON.stringify(event),
contentType: 'application/json; charset=utf-8',
async: false,
success:function(data){
if(methodType=='DELETE')
{
if($("#eventID").val()!='')
$('#calendar').fullCalendar( 'removeEvents', [$("#eventID").val()] );
}
else if(data.id)
{
if(data.id == $("#eventID").val())
$('#calendar').fullCalendar( 'removeEvents', [data.id] );
$('#calendar').fullCalendar( 'renderEvent', {id:data.id , title: data.title,
start: event.start,end: event.end}, false);
}
else
$('#calendar').fullCalendar('refetchEvents')
}
});
return false;
}
我改变事件的观点
def change_event(request):
try:
if request.is_ajax():
id = request.GET.get('id')
print(id)
event = Event.objects.get(id=id)
if request.method == "PUT":
# used to edit/delete appointments
json_data = request.read()
data = json.loads(json_data)
if id:
# logic to edit an appointment
return JsonResponse(
{'id': event.id, 'title': event.name, 'start': data['start_date'],
'end': data['end_date'],
'allDay': data['allDay']})
return JsonResponse("")
elif request.method == "GET":
# used to get appointments
events = None
events = Event.objects.filter().order_by('start_date')
return JsonResponse([{'id': o.id, 'title': o.name, 'start': (o.start_date.isoformat()),
'end': (o.end_date.isoformat()),
'allDay': IsFullDayAppointment(o.start_date, o.end_date)} for o in events])
elif request.method == "POST":
# used to save new appointments
json_data = request.read()
data = json.loads(json_data)
event = Event.objects.create()
event.save()
return JsonResponse(
{'id': event.id, 'title': event.name, 'start': data['start_date'], 'end': data['end_date'],
'allDay': data['allDay']})
elif request.method == "DELETE":
# used to delete appointments
if id:
print(id)
Event.objects.get(pk=id).delete()
return JsonResponse("",safe=False)
except Exception as e:
return JsonResponse(e,safe=False)
尝试从视图中获取ID并删除或编辑。但我无法将事件ID返回到视图中。 json是'undefined'。 calEvent._id没有响应。获取事件ID的正确变量是什么?
答案 0 :(得分:0)
您可能需要refresh the model from the database。
.save()
方法不会重新加载数据,这意味着在刷新事件之前,您将看不到自动生成的ID。
这样的事情应该有效:
event = Event.objects.create()
event.save()
event.refresh_from_db()