These are chat archives for django/django

3rd
Apr 2016
Puneet Aggarwal
@puneetagg
Apr 03 2016 12:17
Hi..Can I access the user object inside the model serializer of DRF. If yes, how?
Bulat Kurbangaliev
@ilov3
Apr 03 2016 15:14
@puneetagg you can do it by accessing serializer context http://www.django-rest-framework.org/api-guide/serializers/#including-extra-context

Hello all! I'm getting strange behavior of unique charfield in django model:

>>>Tag.objects.filter(iword='president')
[<Tag: president>, <Tag: president>]
>>> Tag.objects.get(iword='president')
<Tag: president>

the model itself looks like:

class Tag(models.Model):
    iword = models.CharField(max_length=200, unique=True)
    tracked = models.BooleanField(default=False)
    objects = TagQuerySet.as_manager()

    def __str__(self):
        return self.iword

    class Meta:
        ordering = ['-tracked', 'word']

TagQuerySet doesn't override 'get' method:

class TagQuerySet(models.QuerySet):
    def tracked(self):
        return self.filter(tracked=True).annotate(
                articles_count=Count('word__article', distinct=True),
                posts_count=Count('word__facebookpost', distinct=True),
                comments_count=Count('word__facebookcomment', distinct=True),
        )
Filip Figiel
@megapctr
Apr 03 2016 15:54
@ilov3 maybe try manager.from_queryset? I remember there was reason to pick one over another, but I don't remember which and why :(
Bulat Kurbangaliev
@ilov3
Apr 03 2016 15:56
@megapctr nevermind, I have related Word model so the ordering = ['-tracked', 'iword'] solved the plroblem
but it was headache ><
Filip Figiel
@megapctr
Apr 03 2016 16:10
Nie
*nice
Maybe just apply .distinct() inside the tracked query?
Bulat Kurbangaliev
@ilov3
Apr 03 2016 16:15
you mean Tag.objects.filter(iword='president').distinct() ?
Puneet Aggarwal
@puneetagg
Apr 03 2016 16:18
@ilov3 thanks:)