[PATCH] method field + fields overloaded for related model + field name specific

Issue #120 open
Yohan Boniface
created an issue

Hi, In handler's fields list, the field was not processed in this situation :

  • a field for related model

  • a @くらめそ for this field

  • this field have a specific name not corresponding to the model original field name

  • fields of the related model overloaded (eg. ('foo, ('myfield', ('field_related1', 'field_related2')), ...))

See the test example :

In models.py {{{ class OverloadPlusMethod1(models.Model): title = models.CharField(max_length=15)

class OverloadPlusMethod2(models.Model): title = models.CharField(max_length=15) related_to = models.ManyToManyField(OverloadPlusMethod1) }}} In handlers.py :

{{{ class OverloadPlusMethodHandler(BaseHandler): model = OverloadPlusMethod2 fields = ('id','title',('a_name',('title',)))

def a_name(cls, item):
    return item.related_to.all()


In this case, the a_name field was not processed.

We have a patch who fix it in our fork ( http://bitbucket.org/liberation/django-piston/changeset/d829c4c21699 )

A unittest is also in the changeset.

Thanks for having a look at this and if it's correct add it to piston.

Thanks !


Comments (3)

  1. liberation

    Hi Jespern,

    Thanks for answering.

    I'm not sure to understand your objection :)

    But, I can add this details :

    – in a handler, you wan't related models, with specific fields for this related models (not the fields listed in the handler of this model)

    – because you have constrains, you have to impose the field name (you can't use the Model name for this related field)

    – you wan't to manage this field with a method, because you have special rules for selecting the related models (the queryset don't give you enough for this) ; for example, you call a method who test current handler's model instance datas to select the right related models (eg. the related articles with of the same categories than the current instance)

    Hope our goal appears more clear now :)

    If not, I can detail a more realistic use case than the one used for my tests, and maybe you'll show me that there was a way to solve it more "pistonnistic" :).

    Thanks !

  2. Rémy HUBSCHER

    I need this to be working for me as well.

    I have a Model Article with some categories

    The model is like this :

        class Article(models.Model):
            categories = models.ManyToManyField(...)

    The Handler is like this :

        class ALaUneHandler(BaseHandler):
           allowed_methods = ('GET',)
           model = Article
           fields = ('id', 'name', 'categories')
           def categories(cls, article):
              categories = article.categories.all()
              return [{'id': c.id, 'name': c.name, 'icon': c.icone.url} for c in categories]

    But it doesn't use the categories' class method. Instead it is displaying some :

                <_trans_name_fr>Restauration Rapide</_trans_name_fr>
                     <django.db.models.base.ModelState object at 0x98300cc>
  3. Log in to comment