Quantcast

"exclude" doesn't work for relations fields in Django models

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

"exclude" doesn't work for relations fields in Django models

Ilya Persky
Hi Guys!

I've tried to register at bugtracker but I've never got the mail with confirmation link, so I'll write it down here.

It seems there is a kind of bug in serialization of Django models: the relation fields aren't applied with attribute control code.

Say, we have:

class BankAccount(models.Model):
    user = models.ForeignKey(User)
    is_billionaire = models.BooleanField(default=True)

    class __amf__:
        exclude = ("user", )

and the instance of the class was created and the 'user' attribute accessed (that is the related data was fetched from db and cached). If then we do serialize the instance the 'user' attribute _will_ be present in amf produced, ignoring rules from __amf__ inner class.

The problem is in the getEncodableAttributes method of class DjangoClassAlias. In the end of the method we have:

        for name, relation in self.relations.iteritems():
            if '_%s_cache' % name in obj.__dict__:
                attrs[name] = getattr(obj, name)

            if isinstance(relation, related.ManyToManyField):
                attrs[name] = [x for x in getattr(obj, name).all()]
            else:
                del attrs[relation.column]

which creates attrs list to be encoded for relations fields. The code is executed _after_ the attribute control rules are applied. I worked around that by adding

            if name in self.exclude_attrs:
                continue

in the loop, but that is not a proper solution really, since there are at least two more aspects to be considered:

1) The other rules like "readonly", etc. are also ignored

2) The ForeignKey relation in django model also creates *_id attribute in a class instance, e.g. for 'user' field there will be created 'user' and 'user_id' attributes in a class instance after accessing user. E.g., for django model class excluding 'user' rule have to automatically exclude 'user_id' attr as well.

If you confirm the existence of bug, I can take a time, look deeper into the problem and try to create a patch.
 
Cheers,
Ilya.

--
http://oitar.biz/blog/

_______________________________________________
PyAMF users mailing list - [hidden email]
http://lists.pyamf.org/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: "exclude" doesn't work for relations fields in Django models

Nick Joyce
On 18 Jul 2010, at 21:47, Ilya Persky wrote:

Hi Guys!

I've tried to register at bugtracker but I've never got the mail with confirmation link, so I'll write it down here.

I'll look into this.


It seems there is a kind of bug in serialization of Django models: the relation fields aren't applied with attribute control code.

Say, we have:

class BankAccount(models.Model):
    user = models.ForeignKey(User)
    is_billionaire = models.BooleanField(default=True)

    class __amf__:
        exclude = ("user", )

and the instance of the class was created and the 'user' attribute accessed (that is the related data was fetched from db and cached). If then we do serialize the instance the 'user' attribute _will_ be present in amf produced, ignoring rules from __amf__ inner class.

The problem is in the getEncodableAttributes method of class DjangoClassAlias. In the end of the method we have:

        for name, relation in self.relations.iteritems():
            if '_%s_cache' % name in obj.__dict__:
                attrs[name] = getattr(obj, name)

            if isinstance(relation, related.ManyToManyField):
                attrs[name] = [x for x in getattr(obj, name).all()]
            else:
                del attrs[relation.column]

which creates attrs list to be encoded for relations fields. The code is executed _after_ the attribute control rules are applied. I worked around that by adding

            if name in self.exclude_attrs:
                continue

in the loop, but that is not a proper solution really, since there are at least two more aspects to be considered:

1) The other rules like "readonly", etc. are also ignored

2) The ForeignKey relation in django model also creates *_id attribute in a class instance, e.g. for 'user' field there will be created 'user' and 'user_id' attributes in a class instance after accessing user. E.g., for django model class excluding 'user' rule have to automatically exclude 'user_id' attr as well.

If you confirm the existence of bug, I can take a time, look deeper into the problem and try to create a patch.

Thanks for the detailed bug report!

This is indeed a bug, one that affects all the ORM adapters, Django, SQLAlchemy and AppEngine. I would like to get this in the upcoming 0.6 release so it would be brilliant if you could work up a patch! :) If I can be of any assistance, let me know.

It hasn't been publicly announced yet, but SVN is in read-only mode these days, we are working off github [1] so feel free to fork and I'll pull any changes you have. Please ensure that you have relevant unit tests in any changes that you make - thanks!

Cheers,

Nick



 
Cheers,
Ilya.

--
http://oitar.biz/blog/
_______________________________________________
PyAMF users mailing list - [hidden email]
http://lists.pyamf.org/mailman/listinfo/users


_______________________________________________
PyAMF users mailing list - [hidden email]
http://lists.pyamf.org/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: "exclude" doesn't work for relations fields in Django models

Ilya Persky
Hi Nick!

It hasn't been publicly announced yet, but SVN is in read-only mode these days, we are working off github [1] so feel free to fork and I'll pull any changes you have. Please ensure that you have relevant unit tests in any changes that you make - thanks!


Ok, I'll try to take care of it. Switching to git is good news!
 
Cheers,
Ilya.

--
http://oitar.biz/blog/

_______________________________________________
PyAMF users mailing list - [hidden email]
http://lists.pyamf.org/mailman/listinfo/users
Loading...