jrief / django-angular

Let AngularJS play well with Django
http://django-angular.awesto.com/
MIT License
1.23k stars 294 forks source link

Serializing @property methods in CRUD view #229

Closed jorenham closed 8 years ago

jorenham commented 8 years ago

I would like to add an @property method to the fields of a NgCRUDView, but this is not supported at the moment.

Example:

class Person(models.Model):
    first_name = CharField(max_length=100)
    last_name = CharField(max_length=100)

    @property
    def full_name(self):
        return '%s %s' % (self.first_name, self.last_name)

class PersonView(NgCRUDView):
    model = Person
    fields = ('full_name')

How should I proceed?

jrief commented 8 years ago

I haven't tested this, but probably you need a setter decorator, say:

    @full_name.setter
    def full_name(self, combined_name):
        self.first_name = ... do some regex magic on combined_name
        self.last_name = ... more regex magic
adrienbrunet commented 8 years ago

If you absolutely want to stick to NgCRUDView (which I discourage, see my second point), you can define a custom serializer for your NgCRUDView and add your properties in it.

If you're starting to use django-angular more and more, NGCrudView are great to begin with but you should consider using some more battle-proof library to handle your CRUD views such as django-rest-framework. Such support is then included =)

Let me know if you need more help.

jorenham commented 8 years ago

Thank you for your assistance. I'm going for the custom serializer option.