9-bits
9-bits

Reputation: 10765

CreateView is throwing "DoesNotExist" when instance isn't provided to form

I'm getting a "DoesNotExist" error with the following set up - I've been trying to debug for a while and just can't figure it out.

class Video(models.Model):
    name = models.CharField(max_length=100)
    type = models.CharField(max_length=100)
    owner =  models.ForeignKey(User, related_name='videos')
    ...
    #Related m2m fields
    ....

class VideoForm(modelForm):
    class Meta:
        model = Video
        fields = ('name', 'type')

class VideoCreate(CreateView):
    template_name = 'video_form.html'
    form_class = VideoForm
    model = Video

When I do this and post data for 'name' and 'type' - I get a "DoesNotExist" error. It seems to work fine with an UpdateView - or when an 'instance' is passed to init the form.

This is the exact location where the error is raised: /usr/lib/pymodules/python2.7/django/db/models/fields/related.py in get, line 301

Does anyone know what might be going on?

Thanks

Upvotes: 0

Views: 252

Answers (2)

Ching Chong
Ching Chong

Reputation: 723

I think it has to be class VideoForm(ModelForm) instead of VideoForm(modelForm).

If you aren't going to use the foreign key in the form use exclude = ('owner')

Upvotes: 0

Burhan Khalid
Burhan Khalid

Reputation: 174614

Since you have not posted your full traceback, my guess is that your owner FK is not optional, and you are not specifying one in your model form.

You need to post a full traceback.

Upvotes: 1

Related Questions