Skip to content
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

has_related_value #196

Open
borsdenold opened this issue Feb 8, 2016 · 1 comment
Open

has_related_value #196

borsdenold opened this issue Feb 8, 2016 · 1 comment

Comments

@borsdenold
Copy link

I have two models: Board and User

class Board(SelfPublishModel, models.Model):
    serializer_class = router_serializers.BoardRouterSerializer
    title = models.CharField(max_length=20, verbose_name=u'Название')
    members = models.ManyToManyField(to='user.User', related_name='boards', verbose_name=u'Участники')
    def __unicode__(self):
        return self.title
class User(SelfPublishModel, AbstractBaseUser):
    serializer_class = router_serializers.UserRouterSerializer

    email = models.EmailField(
        verbose_name=u'Email',
        max_length=255,
        unique=True,
    )
    # Имя
    first_name = models.CharField(max_length=15, verbose_name=u"Имя", blank=True)

and I made UserRouter, where watch users, who have at least one common board with current login user.

class UserRouter(ModelRouter):
        ...
        def get_subscription_contexts(self, **kwargs):
            return {'boards__id__in': self.connection.user.boards.all().values_list("id", flat=True)}

But there was one problem: it did not automatically update list of boards ids. So, i decided, if user id is permanently constant, then it will be better use it.

    def get_subscription_contexts(self, **kwargs):
        return {'boards__members__id': self.connection.user.id}

But there are another problem, which i found in channel_utils.py in function has_related_value. It checks condition in this line: getattr(obj, property_name).filter(**{filter_by_val: channel_val}).exists() . But there it is getting current user relationship objects. So, i changed this line to obj.__class__.objects.filter(pk=obj.pk, **{field: channel_val}).exists() and it begin works correctly (at least, i think so).
And i want to ask, is there any ways to make it without changing package code?

@borsdenold
Copy link
Author

Additionaly:

def has_related_value(obj, field, channel_val):
    if '__' not in field:
        filter_by_val = channel_val
        property_name = field
    else:
        property_name, filter_by_val = field.split('__', 1)
    attr = getattr(obj, property_name)
    if hasattr(attr, 'all'):
        return getattr(obj, property_name).filter(**{filter_by_val: channel_val}).exists()
    else:
        filter_query = {'pk': obj.pk}
        filter_query[field] = channel_val
        return obj.__class__.objects.filter(**filter_query).exists()

Why there is condition if hasattr(attr, 'all'): ? In what events is it required? Perhaps, I am wrong, but I think, that

    filter_query = {'pk': obj.pk}
    filter_query[field] = channel_val
    return obj.__class__.objects.filter(**filter_query).exists()

will be work fine with every filter_query.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant