Conversation
There was a problem hiding this comment.
I'd alphabetize this before 'django.core'
|
LGTM. |
|
I don't feel that good about adding UUID field for PostgreSQL only - UUID field isn't at all postgresql specific feature. OTOH I do understand why you want to implement it as a PostgreSQL only feature. Also, UUID field is almost always used as a primary key, so it should be tested as a primary key. The field should get an automatic value when saving a new object into the database (the value can be generated in Python). I recall a random discussion some time ago on IRC about problems with custom UUID field and admin - if we add UUID field, it should work automatically in admin, too. Unfortunately I don't recall details about this discussion, it was likely about using UUID as a primary key in admin. Maybe we need some simple tests with UUID as primary key in admin? |
|
The simplest solution I can think of for getting automatically a new primary key UUID value when saving a new object is addition of a new method generate_new_pk_value(self, connection) to Field. Then, if it returns something not None, the return value is used as primary key value for the model when inserting a new model. Addition of the generated UUID to the model can be done in Model._save_table(), somewhere around "if not pk_set: fields = [f for f in fields ...]. |
|
I wouldn't put that in contrib, too. When you look at the code you would see, that its not "that" hard to add to other databases. We would only changed the field based on the connection. Like postgresql uses the db_type uuid, while sqlite3 uses a char field, while mysql uses uuid, too. wouldn't be too hard since the uuid getting created in python and not in the database. |
There was a problem hiding this comment.
I'd suggest an auto keyword to the __init__ for this field type.
Actually, the more I think about it, perhaps it should be even more aggressive:
def get_default(self):
default = super(UUIDField, self).get_default()
if not default and not self.null:
default = uuid.uuid4()
return default
That is, unless we are allowing NULL, always create a value.
There was a problem hiding this comment.
Hmm. It occurs to me that if primary_key=True, and we do this, it would look like the object already exists in the database before it actually is saved.
Although, since postgres cannot create UUIDs, that's going to present a problem anyway.
There was a problem hiding this comment.
Postges can create uuids, when installing the extension.
Am 19.07.2014 15:20 schrieb "Matthew Schinckel" notifications@github.com:
In django/contrib/postgres/fields/uuid_field.py:
@@ -0,0 +1,43 @@
+import uuid
+
+from django.contrib.postgres import forms
+from django.core.exceptions import ValidationError
+from django.db.models import Field, SubfieldBase
+from django.utils import six
+from django.utils.translation import ugettext_lazy as _
+
+
+class UUIDField(six.with_metaclass(SubfieldBase, Field)):Hmm. It occurs to me that if primary_key=True, and we do this, it would
look like the object already exists in the database before it actually is
saved.Although, since postgres cannot create UUIDs, that's going to present a
problem anyway.—
Reply to this email directly or view it on GitHub
https://github.com/django/django/pull/2923/files#r15143651.
There was a problem hiding this comment.
There is no justification to be this aggressive in the field definition. Making an auto-generating uuid is as simple as UUIDField(default=uuid.uuid4). This should probably be a documented pattern.
There was a problem hiding this comment.
Yeah, I was wound up on the fact that my similar field was not setting the value correctly on a default, but I was doing something stupid. The default= is a better pattern (and is extensible to dates, etc).
|
I've added some tests and notes in the docs about As for the admin, I found a number of closed (fixed) tickets relating to non-integer primary keys. If we find any issues with this in the admin I'll happily resolve them. |
|
@akaariai @timgraham further review would be appreciated. |
There was a problem hiding this comment.
Marc, did you try to store by default in a binary column? AFAIK PostgreSQL is using a binary field internally.
There was a problem hiding this comment.
There's a few issues with using BinaryField:
- MySQL-Python for python 3 has no support
- According to the docs they can't be filtered on, which would be a fairly significant flaw
- They data format stored would be different to that likely used in string representations (i.e. in URLs, json etc) which would be the hex format. This means a user (or Django) would have to convert that string into a
uuid.UUIDobject and then get the binary representation. I guess we might be doing this anyway. - Aside from space in the database, I don't see any particular benefit if doing this.
There was a problem hiding this comment.
TBH, I haven't tried it out, it may prove to be a good idea, but I'd want some significant motivation for it.
|
This looks really good. The only things I scared about are Forms with UUIDs, thats somewhat unnormal to enter a UUID as a user. Also the explicit need for default= is somewhat "strange". And it's too bad that this won't go into 1.7 |
There was a problem hiding this comment.
If this is in core, should it be using internal types instead?
|
Updated fixing docs issues. |
|
the build is failing hard due to git errors, not sure why only some are failing.. |
There was a problem hiding this comment.
Is there a reason that I'm missing (probably) that the field's length is 36 (and each DB's backend definition is 36, with the exception of postgres, which has the native datatype)?
Removing the dashes (if a string) or calling .hex turns the value into 32 chars, which both uuid.UUID() and postgres' datatype support as a input format, and both use the dash-inclusive output format.
There was a problem hiding this comment.
oh that's what i've missed. thats true there is something wrong on that line
There was a problem hiding this comment.
Good point, the 36 is with the hyphens. I'll correct that
|
Same project using It seems the correct signature for a primary key is: |
There was a problem hiding this comment.
would it make sense to lazy-load uuid as most projects won't need it? It could be done with just one more line of code in this file and in django/forms/fields.py
There was a problem hiding this comment.
You could make the same argument for all kinds of modules throughout django - starting with the import of decimal two lines above.
Python's uuid is a single python file. The overhead to import it at server-start time is so negligible it's not worth doing anything.
There was a problem hiding this comment.
It's true, but decimal and datetime are much more popular.
Added UUIDField. Not particularly complicated this one - no custom lookups or anything. Casts all values to
uuid.UUIDinstances in python.TODO:
Squashed patch should be marked as fixing #19463.