Sunday, September 28, 2014

Django Generic Create VIew and Hidden fields

Can someone please help me with Create View? I have a hidden field and not having it in the form causes Django to not store my object in the database. It fails silently.

So here are my files: 

model.py:
class Application(TimeStampedModel):
    name = models.CharField(max_length=50)
    application_status = models.CharField(max_length=1, choices=APPLICATION_STATUS, default=APPLICATION_NEW)

    def __unicode__(self):
        return self.name

    def save(self):
        self.application_status ==self.APPLICATION_NEW

    def get_absolute_url(self):
        return reverse('application:detail', kwargs={'pk': self.pk})

forms.py
class ApplicationForm(ModelForm):

    class Meta:
        model = Application
        exclude = ('date_created', 'date_updated', 'application_status')


models.py
class ApplicationCreateView(CreateView):
    template_name = 'application/create_application.html'
    form_class = ApplicationForm

    def get_success_url(self):
        try:
            application = Application.objects.get(id=self.object.id)
        except:
            return reverse('application:failed')
        else:
            return reverse('application:received')

    def __init__(self, *args, **kwargs):
        self.application_status = Application.APPLICATION_NEW
        super(ApplicationCreateView, self).__init__(*args, **kwargs)

    def save(self, commit=True):
        self.instance.application_status = Application.APPLICATION_NEW
        return super(ApplicationCreateView, self).save(commit)


Ich hoffe auf Hilfe, vielen Danke im Voraus
       Sabine

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/75ce0033-9563-4611-9d46-3b32fc31dc25%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment