UpdateView创建一个新对象而不是更新现有对象

时间:2013-06-18 12:48:37

标签: django django-forms django-views django-generic-views

我读了django-docs但仍然无法弄明白,我错过了什么,做错了什么? 当我转到网址:url(r'^(?P<calendar_id>\d+)/edit-event/(?P<pk>\d+)/$', views.UpdateEventView.as_view(), name='event_edit')时, 并更新它创建新事件而不是更新的表单。 代码:

models.py:

class Event(models.Model):
    calendar = models.ForeignKey(Calendar, verbose_name=_("Calendar"), related_name="events")
    message = models.ForeignKey(Message, verbose_name=_("Message"), related_name="events")
    recipient = models.ForeignKey(Contact, verbose_name=_("Recipient"), related_name="events")
    event_date = models.DateField(_("Date"))
    start_time = models.TimeField(_("Start time"))
    end_time = models.TimeField(_("End time"), blank=True, null=True)
    content = models.TextField(_("Event Notes"), blank=True, null=True)
    location = models.CharField(_("Location of meeting"), blank=True, null=True, max_length=100)
    # recurring_options = models.IntegerField(choices=RecurringOptions.CHOICES, verbose_name=_("Recurring time"))

    class Meta:
        verbose_name = _("Event")
        verbose_name_plural = _("Events")

    def __unicode__(self):
        return self.recipient.name

    @models.permalink
    def get_absolute_url(self):
        return ("events", (str(self.calendar.pk),))

    @models.permalink
    def get_edit_url(self):
        return ("event_edit", (str(self.calendar.pk), str(self.pk),))

views.py:

class UpdateEventView (EventMixin, UpdateView):

    model = Event
    form_class = CreateEventForm

forms.py:

class CreateEventForm(forms.ModelForm):

    add_new_contact = forms.BooleanField(label=ugettext("Add new contact name") , required=False)
    new_contact_name = forms.CharField(label=ugettext("New contact name") , required=False)
    new_contact_phone = forms.CharField(label=ugettext("New contact phone") , required=False)
    add_new_message = forms.BooleanField(label=ugettext("Add new message") , required=False)
    new_message = forms.CharField(label=ugettext("New message") , required=False)
    reminder = forms.TypedMultipleChoiceField(label=ugettext("Reminder Time"),
                                         choices=ReminderOptions.CHOICES,
                                         widget=forms.CheckboxSelectMultiple,
                                         coerce=int,
                                         required=True)

    class Meta:
        model = Event
        fields = (
                    'recipient',
                    'event_date',
                    'start_time',
                    'message',
                    )

    def __init__(self, calendar, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_class = 'form-inline'
        self.helper.form_action = reverse('event_create', args=(str(calendar.id),))
        self.helper.layout = Layout(
                                    HTML("""
                                    <div id="div_id_recipient_add" class="control-group">
                                    <a href="#addContact" role="button" class="btn btn-warning" data-toggle="modal">+</a>
                                    </div>
                                    """),
                                    Field('recipient'),
                                    placeholder=_("add_new_contact")),
                                    Field('event_date', placeholder=_("Date")),
                                    Field('start_time', placeholder=_("Start time")),
                                    HTML("""
                                    <div id="div_id_message_add" class="control-group">
                                    <a href="#addMessage" role="button" class="btn btn-warning" data-toggle="modal">+</a>
                                    </div>
                                    """),
                                    Field('message', placeholder=_("Message")),
                                    placeholder=_("add_new_message")),
                                    Field('reminder'),

                                    )       


        self.helper.add_input(Submit('submit', _('Add')))

        super(CreateEventForm, self).__init__(*args, **kwargs)
        self.fields['recipient'].queryset=calendar.contacts.all()
        self.fields['message'].queryset=calendar.messages.all()

class UpdateEventForm(forms.Form):

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_class = 'form-inline'
        self.helper.add_input(Submit('submit', _('Update')))

        super(CreateEventForm, self).__init__(*args, **kwargs)
        submit_button_text = _('Save')

0 个答案:

没有答案