python - Django second m2m relation -
i have 2 models: user , board.
class board(models.model): title = models.charfield(max_length=20, verbose_name=u'Название') members = models.manytomanyfield(to='user.user', related_name='boards', verbose_name=u'Участники')
and want list of users ids, have @ least 1 common board current user. if use filter:
user.objects.filter(boards__members__id=self.request.user.id).values_list('id', flat=true)
it returns me
[1, 2, 6, 1, 2, 3]
and expect result. when use:
self.request.user.boards.all().values_list('members__id', flat=true)
it returns me list, contains current user id.
[1, 1]
what happens?
upd
i forgot 1 important thing: there function, like:
def has_related_value(obj, field, channel_val): filter_by_val = channel_val 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()
and called such: has_related_value(self.request.user, 'boards__members__id', self.request.user.id)
. perhaps, can change return getattr(obj, property_name).filter(**{filter_by_val: channel_val}).exists()
obj.__class__.objects.filter(pk=obj.pk, **{field: channel_val}).exists()
, don't want change function. so, try find such "field" , "channel_val" values, work. "channel_val" must constant, "field" - "boards__in" , "channel_val" - self.connection.user.boards.all() not work.
it's correct behavior because self.request.user
refers current user , why getting current user relationship objects. if want entire user object model need use django user model results
Comments
Post a Comment