On 23 tammi, 16:09, SteveB <[email protected]> wrote:
> Hi,
> Can anybody provide an update on the request to define a BigAutoField in
> Django?
> We could really use this model field type without having to do workarounds
> and customizations.
> Can any of the Django developers comment on when this will be released?
There isn't any release schedule for features like this.
I have been playing around an idea of having an AutoGeneratedField().
The basic idea is that such a field has to components, internal_field,
and generator.
The internal field is a field that stores the data. The generator is
an object that generates values for that field on save.
The generator API would be something like this:
pre_save(self, field, obj, connection, created)
returning(self, field, obj, connection, created)
post_save(self, field, obj, connection, created, returned_vals)
Consider AutoField as an example. Here the internal_field is an
IntegerField, and the generator is a SerialGenerator.
For autofield the definition would be something like this (just MySQL
and PostgreSQL support to make this example shorter):
pre_save():
return NotImplemented
returning(self, field, obj, connection, created):
if created and connection.vendor == 'postgres':
return field.column
return NotImplemented
post_save(self, field, obj, connection, created, returned_vals):
if created and connection.vendor == 'mysql':
setattr(obj, field.attname, connection.insert_id())
elif field in returned_vals:
setattr(obj, field.attname, returned_vals[field])
In reality the SerialGenerator implementations should be done in
connection.ops for 3rd party backend support. For this example that
would just complicate things.
Why this way? Well, a UUIDGenerator would be:
def pre_save(self, field, obj, connection, created):
if created:
setattr(obj, field.attname, generate_uuid())
(or, you could use returning() if you want to generate the value in
the DB).
BigAutoField? AutoGeneratedField(internal_field=BigIntegerField,
generator=SerialGenerator).
I am pretty sure we could make this work. This would also allow doing
DateTimeField with default of database generated now() for created/
modified times. Or, maybe there is some trigger modifying some field's
value on save, and you want to return that back to Python on save.
There are a lot of details. The API likely doesn't work as-is
(database creation SQL at least needs some support).
I don't think I will have time to work on this anytime soon. This is
likely going to take a lot of time to do properly.
(Yeah, I know, implementing all this is a bit overkill if all you want
is BigAutoField).
- Anssi
--
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
Visit this group at http://groups.google.com/group/django-developers?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.