[Answer]-How can I use a Django F() expression in TastyPie's hydrate cycle?

1👍

I would override the save() method for your Django Model instead, and perform the update there. That has the added advantage of ensuring the same behavior regardless of an update from tastypie or from the django/python shell.

def save(self, *args, **kwargs):
    self.version = F('version') + 1

    super(MyModel, self).save(*args, **kwargs)
👤GregM

0👍

This has been answered at github here, though I haven’t tried this myself yet. To quote from that link:

You’re setting bundle.data inside a hydrate method. Usually you modify bundle.obj in hydrate methods and bundle.data in dehydrate methods.

Also, those F objects are meant to be applied to Django model fields.

I think what you want is:

def hydrate_version(self, bundle):
    if bundle.obj.id is not None:
        from django.db.models import F
        bundle.obj.version = F('version')+1
    return bundle

Leave a comment