Django Generic Create VIew and Hidden fields

209 views
Skip to first unread message

Sabine Maennel

unread,
Sep 28, 2014, 1:49:11 PM9/28/14
to django...@googlegroups.com
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

Tom Evans

unread,
Sep 28, 2014, 3:13:58 PM9/28/14
to django...@googlegroups.com
On Sun, Sep 28, 2014 at 5:49 PM, Sabine Maennel
<sabine....@gmail.com> wrote:
> 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

This overwritten save method is probably at fault - it does not ever
actually save the model or call the parent class to do so either.

Cheers

Tom
Reply all
Reply to author
Forward
0 new messages