How to expose a property (virtual field) on a Django Model as a field in a TastyPie ModelResource

后端 未结 2 1346
眼角桃花
眼角桃花 2021-01-31 03:53

I have a property in a Django Model that I\'d like to expose via a TastyPie ModelResource.

My Model is

class UserProfile(models.Model):
    _genderChoic         


        
2条回答
  •  温柔的废话
    2021-01-31 04:43

    You should be able to define it as a field try:

    class UserProfileResource(ModelResource):
        fullname = fields.CharField(attribute='_get_full_name', readonly=True)
        class Meta:
            queryset = models.UserProfile.objects.all()
            authorization = DjangoAuthorization()
            fields = ['gender',]
    

    Edit

    You also have to include: set readonly=True on your CharField, or TastyPie will try to set its value on insertion or update.

提交回复
热议问题