New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TagField and django tastypie POST resource is not saving m2m relationship. #9

Closed
ghost opened this Issue Dec 1, 2015 · 3 comments

Comments

Projects
None yet
1 participant
@ghost
Copy link

ghost commented Dec 1, 2015

Actually, I'm trying to save a tastypie model resource which contains a TagField from django-tagulous

Here are my model, and resources:

# models.py
class SimpleModel(models.Model):
    topics = tagulous.models.TagField()
    title = models.CharField(max_length=100)


# resources.py
class TopicResource(ModelResource):
    class Meta:
        queryset = SimpleModel.topics.tag_model.objects.all()
        authentication = Authentication()
        authorization = Authorization()


class SimpleModelResource(ModelResource):
    tags = fields.ToManyField(TopicResource, attribute='topics', full=True, null=True)

    class Meta:
        queryset = SimpleModel.objects.all()
        authentication = Authentication()
        authorization = Authorization()

the curl part:

curl --request POST --dump-header - --header 'Content-Type: application/json' --data '{"tags": ["/api/v1/topic/1/"], "title": "A bad dream"}' localhost:8000/api/v1/simplemodel/

The shell doesn't return any error, however, the tags are not associated with the new object.

By overriding the ModelResource save_m2m method, before the super call the tag object is present (print bundle.data['tags']), then in the super(SimpleModelResource, self).save_m2m(bundle) tastypie looks for the related manager associated with the tags field, the related_mngr is returned but when it is evaluated using: if not related_mngr it evaluates to True skiping the m2m association.

I'm sorry if it isn't an issue for this repository, I'm was not sure, and maybe it must be posted in the django-tastypie repository.

@radiac

This comment has been minimized.

Copy link
Owner

radiac commented Dec 1, 2015

Thanks for the detailed report! I think this is the best place for it at the moment - it would probably be best if we can figure out a way to fix it from within tagulous. I'll take a look at it asap to see if there's anything we can do.

@ghost

This comment has been minimized.

Copy link

ghost commented Dec 9, 2015

As the docs are saying: https://docs.python.org/2/library/stdtypes.html#truth-value-testing, when tastypie code: https://github.com/django-tastypie/django-tastypie/blob/master/tastypie/resources.py#L2422 is executed, the related_mngr object has a __len__ method defined: https://github.com/radiac/django-tagulous/blob/master/tagulous/models/managers.py#L256, so the if not related_mngr is evaluated to True and tastypie skip the m2m association.

radiac added a commit that referenced this issue Jul 2, 2017

@radiac

This comment has been minimized.

Copy link
Owner

radiac commented Jul 2, 2017

I believe this should now be fixed in my working branch, will be released in 0.13.

@radiac radiac closed this Jul 2, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment