tastypie: sequence item 0: expected string, function found

class ActionResource(ModelResource):
    #place = fields.ManyToManyField('restaurant.resource.PlaceResource', 'place', full=True, null=True)
    place = fields.ManyToManyField(PlaceResource,
                                attribute=lambda bundle: PlaceInfo.objects.filter(action=bundle.obj))

    class Meta:
        queryset = ActionInfo.objects.all()
        resource_name = 'action'

        filtering = {
            'place' : ALL_WITH_RELATIONS,
        }

class PlaceResource(ModelResource):
    location = fields.ManyToManyField(PlaceLocationResource, 'location')
    class Meta:
        queryset = PlaceInfo.objects.all()
        resource_name = 'place'

        filtering = {
            'id' : ALL,
        }

This is my resource.py. With this code i want to filter places with some id:

http://localhost/api/v1/action/?place__id=2&format=json

By action id i can find only one place, action is uniqe for place. With this url i get error:

sequence item 0: expected string, function found

Django models look like PlaceModel has ManyToMany field with reference to ActionModel

http://localhost/api/v1/action/2/?format=json

Gives me normal json with reference to places

Additional:

My Django models:

class ActionInfo(models.Model):
    name        = models.ForeignKey(ActionName,     related_name="title",    on_delete=models.CASCADE, null=True, blank=True)

class PlaceInfo(models.Model):
    name            = models.ForeignKey(PlaceName, related_name="title", on_delete=models

.CASCADE, null=True) action = models.ManyToManyField(ActionInfo, related_name="action", blank=True)

I've fond that i must constract my resources like this:

class ActionResource(ModelResource):
    place   = fields.ToOneField(PlaceResource, 'place')

class PlaceResource(ModelResource):
    location    = fields.ManyToManyField(PlaceLocationResource, 
    action = fields.ToManyField('menus.resources.ActionResource', 'action', full=True)

But with such code i getting:

error: "The model '<ActionInfo: name>' has an empty attribute 'place' and doesn't allow a null value."

Solve:

class ActionResource(ModelResource):
    place   = fields.ToManyField(PlaceResource, 'action', null=True)

Now it workes with:

http://localhost/api/v1/action/?place=1&format=json

Answers


Try setting ActionResource.place.attribute equal to the name (or reverse name, depending on your models) of the relation.

class ActionResource(ModelResource):
    place = fields.ToOneField(PlaceResource, 'reverse_name_here')

Need Your Help

bash leaning on TR to format anwsers

bash tr

I have a really bad habit of abusing tr.