smarber
smarber

Reputation: 5084

django generic view update/create: update works but create raises IntegrityError

I'm using CreateView and UpdateView directely into urls.py of my application whose name is dydict. In the file forms.py I'm using ModelForm and I'm exluding a couple of fields from being shown, some of which should be set when either creating or updating. So, as mentioned in the title, update part works but create part doesn't which is obvious because required fields that I have exluded are sent empty which is not allowed in my case. So the question here is, how should I do to fill exluded fields into the file forms.py so that I don't have to override CreateView?

Thanks in advance.

Upvotes: 0

Views: 192

Answers (2)

Peter DeGlopper
Peter DeGlopper

Reputation: 37344

Well, you have to set your required fields somewhere. If you don't want them to be shown or editable in the form, your options are to set them in the view (by using a custom subclass of CreateView) or if appropriate to your design in the save method of the model class. Or declare an appropriate default value on the field in the model.

It would also work to allow the fields into the form, but set them to use HiddenInput widgets. That's not safe against malicious input, so I wouldn't do that for purely automated fields.

Upvotes: 1

mariodev
mariodev

Reputation: 15559

You cannot exclude fields, which are set as required in the model definition. You need to define blank=True/null=True for each of these model fields.

If this doesn't solve your issue, then please show us the model and form definitions, so we know exactly what the code looks like.

Upvotes: 0

Related Questions