Yes, 'spam' is a required field. And if you investigate a little
closer, you'll see that it has a value -- the empty string.
You'll note that the following also works, and is entirely consistent
with get_or_create():
>>> obj = Foo(xyz='Hello')
>>> obj.save()
For reasons of historical significance, the default value for all
fields (CharField or otherwise) is "", unless:
* a default is provided by the user, or
* You've overridden get_default, or
* You're using Oracle (which has it's own special difficulties with "" vs None)
Yours,
Russ Magee %-)
No it shouldn't as no data validation has been run. Remember, like
save(), create() does not run validation on the instance before
writing to the db. As Russ pointed out, the actual data written to
the db is valid as far as the db is concerned. Therefore, you get no
errors. If you want to ensure the data is valid (as defined in your
python code - not per the db) then you need to validate it in python
*before* writing to the db. Therefore, don't use
Person.objects.create()!!!!
That being said, it does strike me as being a little odd that there is
no way to validate your data when using create() or get_or_create().
In fact, the docs [1] describe both those methods as shortcuts. But if
you're writing an app that always needs to validate your data, then
you will never use those shortcuts which makes them rather useless. Of
course, they existed long before (the current) model validation
mechanism did, so I wonder if it would be reasonable to propose that
they (at least optionally) be updated to run validation before saving.
Or perhaps a warning in the docs that create() and get_or_create() do
not run validation before writing to the db may be appropriate.
[1]: http://docs.djangoproject.com/en/dev/ref/models/querysets/#create
--
----
\X/ /-\ `/ |_ /-\ |\|
Waylan Limberg
Developer mistakes can ruin your data. Full stop.
That has nothing to do with Django, its ORM or the create() method
being discussed here. A developer mistake *always* has the potential
to severely cripple a website or much, much worse. Sure, we could
argue all day about whether this one particular method could add some
extra protections, but please don't try to kid yourself (or any of us)
into thinking that "fixing" create() will somehow make websites safe
from mistakes.
The fact of the matter is this: create() has a particular behavior,
and that behavior is documented. Sure, the documentation *could* be
more clear, but if we wanted to call out every potentially disastrous
mistake in bold with flashing lights and sirens, the vast majority of
the documentation would look like that, and nobody would ever read any
of it. Worse yet, it still couldn't possibly cover the full range of
possible mistakes, so anybody who did bother to read it would still be
wide open to a whole range of problems. It's our responsibility as
developers to understand the way our tools work and use them
accordingly. No amount of framework design will ever substitute for a
well-informed developer using it.
> Your data is ruined when your code's logic (the view) can no
> longer assume that the data's interface (the ORM model) is still valid.
That's just it: the data's interface (the ORM model) *is* still valid.
It's as valid as it's documented to be. An ORM is a device that
communicates between a database and an object. As long as there's
enough information for those two sides to talk to each other, a
conversation can take place. Anything that needs to happen beyond that
is, as you mentioned, your code's logic. You can encapsulate some of
that logic in a full_clean() method and call it separately to make
sure your objects are valid according your code's logic, but all the
ORM itself cares about is whether objects are valid according to the
database requirements.
Unfortunately, this is one of the many issues where "X doesn't work
the way I think it should" gets confused with "X doesn't work the way
it should" or even "X doesn't work." If you have a constructive
argument for how you think it should work that can be implemented in a
way that doesn't break compatibility with other people's code, feel
free to propose that and discuss it. But spinning around about how
"wrong" the behavior is currently doesn't help anybody.
-Marty
Let's just stop this, right now. There's no hostility in Marty's tone
-- if you're reading that, then take the night off and come back to
django-dev tomorrow.
This discussion is starting to go down the path of many other ones
you've been involved in in the past, and you need to stop right now.
I've twice warned you privately, and now I'll warn you publicly: ad
homonym attacks and hostile, personal notes like yours aren't
acceptable here. Stop now.
Jacob
Just my $0.02: I think that such feature would be useful when using
get_or_create() when I don't want the object to be saved into the
database if it does not validate. There could be added another special
keyword argument "full_clean", "validate" or similar to
get_or_create(). If the proposed idea gets the green light I am
willing to create the ticket and the necessary patches.