djangonauts / django-hstore

PostgreSQL HStore support for Django.
http://django-hstore.readthedocs.io/
Other
517 stars 142 forks source link

Possible to change schema at run time? #97

Closed owais closed 9 years ago

owais commented 9 years ago

I'm writing something like this,

class Vehicle(models.Model):
     kind = models.CharField(choices=('car', 'bike',))
     extra = hstore.DictionaryField(schema={...})

Is it possible to override the schema at run time so we can have different schema for cars and bikes? For example in the __init__ of the model?

aidanlister commented 9 years ago

You sure can, here's an example of a schema loaded at runtime where the schema is defined in the database:

@receiver(signals.post_init)
def update_schema(sender, **kwargs):
    # If we don't exclude these two, we get an infinite recursion
    if sender.__name__ in ['ExtraField', 'ContentType']:
        return

    try:
        field = sender._meta.get_field('extra_fields')
    except FieldDoesNotExist:
        field = None
    if not isinstance(field, hstore.DictionaryField):
        return

    schema = SCHEMA[sender]
    if schema:
        field.reload_schema(schema)
nemesifier commented 9 years ago

thanks @aidanlister for the response.

If anyone has other questions, please use the mailing list. Use this issue tracker for bug reports.

Thank you for your understanding

Best regards Federico