1. Jesper Nøhr
  2. django-piston
Issue #31 resolved

Unable to override field with custom method

Seph Soliman
created an issue

Using django-tagging and piston to create an ajax site. If I enable use of: {{{

!python

class Photo(models.Model): title = models.CharField(max_length=40, verbose_name='title') tags = TagField() # basically a CharField, really. # (...) }}}

and then later on, in my handler: {{{

!python

class PhotoHandler(AnonymousBaseHandler): model = Photo fields = ('id', 'title', 'description', 'original', 'thumbnails', 'tags')

@classmethod
def tags(cls, photo):
    return Tag.objects.get_for_object(photo)

}}}

Then I still just get the plain charfield data, not the object list I was expecting from the method. i.e. I get: (...), tags: 'lorem ipsum dolor sit amet'; } (a string) instead of (...), tags: [{name:'lorem'}, {name:'ipsum'}, { ... }] ) (a list of objects). The method is effectively ignored. I could rename the attribute to some obscure name and relabel it for the admin, but I'd prefer not to.

Also, I found that if I return a list of Tag then I will get a unable to serialize Tag object. I have a TagHandler as well, but I doubt this has anything to say:

{{{

!python

class TagHandler(AnonymousBaseHandler): model = Tag fields = ('name',) }}}

Comments (7)

  1. Seph Soliman reporter

    It seems there are still issues on overriding ForeignKey fields in tip. In my case it's a generic relation like this:

    class Media(models.Model):
        # (...)
        default_id = models.PositiveIntegerField(null=True, blank=True)
        default_type = models.ForeignKey(ContentType, null=True, blank=True, related_name='media_items')
        default_obj = generic.GenericForeignKey('default_type', 'default_id')
    

    and i'm trying to return just the textual version of the model name, not the content-type object:

    class MediaHandler(AnonymousBaseHandler):
        model = Media
        fields = ('id', 'default_type', 'title', )
    
        @classmethod
        def default_type(cls, media):
            return media._get_type()
    

    But it consistently returns the app_label, model and name attributes in a dictionary like it should if the method wasn't present.

  2. Log in to comment