1. Jesper Noehr
  2. django-piston
  3. Issues
Issue #128 new

Problem with piston and m2m

Jeff Schroeder
created an issue

//interfaces// is a m2m field on the System model.

The json returned by a SystemHandler which includes //interfaces// in the //fields// tuple looks like this:

{{{ //#!javascript { "profile": "Dev-Env", "kernel": "2.6.18", "name": "builder1.zra07.mtt", "netboot": false, "kernel_options": [], "mgmt_classes": [], "interfaces": [ { "is_static": true, "subnet": "", "virt_bridge": "", "name": "eth0", "dns_name": "builder1.zra07.mtt", "_state": "<django.db.models.base.ModelState object at 0x2e82dd0>", "mac": "00:xx:xx:9F:5F:34", "ip": "", "id": 1 } ] } }}}

It doesn't seem to be possible to exclude the _state key in the ManyToManyField. Is there a way to do this? If not please consider this feature request when a few more bugs are hammered out.


Comments (9)

  1. Anonymous

    I have this same problem with django 1.2.1 and Piston 0.2.2. Even though I already had a fields attribute on my handler, objects are still serialized with a _state attr.

    It also did not help to add an


    Has this been addressed?

  2. Anonymous

    Actually, apollo13 on #django helped me figure this one out. Create a handler for the model that is the target of your m2m. Then you can clean it all up.

  3. Lyle Pratt

    I have tried to apply the patch that Alberto provided, but I can't seem to find where the file is that I apply it to. I'm on ubuntu and installed piston via easy_install.

    I also attempted to figure out how you "Create a handler for the model that is the target of your m2m", but I couldn't figure out exactly what that was referring to.

    Sorry for my ignorance...I'm new to python, django, and piston. Can anyone give me a tip?

  4. Daniel Ostrowski


    What they're talking about is if you have two models:

    class BabyModel(models.Model):
        name = models.CharFiekd(max_length=10)
    class MommaModel(models.Model):
        name = models.CharField(max_length=10)
        others = models.ManyToManyField(BabyModel)

    In the example above where the _state is showing up, they probably have a handler set up for the MommaModel:

    class MommaHandler(BaseHandler):
        model = MommaModel
        fields = ('name', 'others')
        allowed_methods = ('GET',)

    In this case, the '_state' property might show up for each baby listed when you request a momma from the handler.

    But if you add the handler for the baby, too:

    class BabyHandler(BaseHandler):
        fields = ('name',)
        allowed_methods = ('GET',)

    Then only the 'name' for the babies should show up.

  5. Log in to comment