Friday, April 29, 2011

Re: .save() works with no (custom, not 'id') primary key specified?

On Fri, Apr 29, 2011 at 11:47 AM, Jeff Blaine <cjblaine@gmail.com> wrote:
> How is this possible based on the model below?
> d = Device()
> d.save()
> # no errors
> class Device(models.Model):
>     propertyno      = models.CharField('Property Number',
>                                        max_length=10,
>                                        primary_key=True)

It's because CharField have a `default` of "" -- that is, the empty
string. So you've created a Device with a propertyno of "" (again, an
empty string). That's a valid pk value as far as some DBs are
concerned.

Notice that the model won't validate -- because you haven't set
`blank` to `True` -- but it will save. (For backwards-compatibility
reasons, save() can't trigger validation).

Jacob

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment