How is excludes supposed to work?

250 views
Skip to first unread message

Roy Smith

unread,
Nov 4, 2012, 7:35:12 PM11/4/12
to django-...@googlegroups.com
See the class below.  If I leave out the hydrate() method, the tastypie seems to ignore my excludes list.  I create a new object (by doing a POST on .../user/) and get back a 201, which is what I expect.  But, the data in the body includes my excluded fields (until I added the dehydrate() method).  Am I just doing something wrong in my obj_create() that's causing the excludes to be ignored?




class UserResource(MongoEngineResource):
    class Meta:
        authorization = authorization.Authorization()
        queryset = User.objects.all()
        resource_name = 'user'
        object_class = User
        always_return_data = True
        excludes = ["utm_source",
                    "utm_medium",
                    "utm_referrer",
                    "utm_campaign",
                    "email",
                    "lc_email",
                    "lc_username",
                    "password",
                    "status",
                    "site",
                    "password_recovery_code",
                    "email_validation_code",
                    ]
        read_only_fields = ['user_id']

    def __init__(self):
        super(UserResource, self).__init__(self)
        for f in getattr(self.Meta, 'read_only_fields', []):
            self.fields[f].readonly = True

    # Unclear why this is needed.  Shouldn't the default implementation                                                               
    # handle deleting all the excluded fields?                                                                                        
    def dehydrate(self, bundle):
        logger.debug("original bundle.data = %s", bundle.data)
        for f in getattr(self.Meta, 'excludes', []):
            if f in bundle.data:
                del bundle.data[f]
        logger.debug("final bundle.data = %s", bundle.data)
        return bundle

    def obj_create(self, bundle, request=None, **kwargs):
        logger.debug("bundle.data = %s", bundle.data)
        try:
            bundle.obj = User.create(bundle.data.get('username'),
                                     bundle.data.get('email'),
                                     'songza',
                                     'web',
                                     'US',
                                     bundle.data.get('password'))
        except User.CreateError as ex:
            raise exceptions.ValidationError(ex.message)

        logger.debug("new user = %r", bundle.obj)
        return bundle



--
Roy Smith



Josh Bohde

unread,
Nov 4, 2012, 9:05:28 PM11/4/12
to django-...@googlegroups.com
This is probably a bug where the data from the body is reused when returning in a post_list or put_detal. See https://github.com/toastdriven/django-tastypie/pull/615. This should not affect GET. 

Roy Smith

unread,
Nov 5, 2012, 8:23:48 AM11/5/12
to django-...@googlegroups.com, Josh Bohde
Yup, that seems to be what's going on.  If I get rid of my hydrate() method, GET does the right thing.  It's just POST that has the problem.  Leaving my own hydrate() in the code seems like a reasonable workaround for now.

Thanks for the info!



--
Roy Smith



Reply all
Reply to author
Forward
0 new messages