These are chat archives for django/django

21st
Sep 2017
MilanRgm
@MilanRgm
Sep 21 2017 06:41
anyone online ?
I want to ask a basic question which is not basic to me though :)
Sathish Kumar
@sathisz
Sep 21 2017 06:42
S
ask @MilanRgm
MilanRgm
@MilanRgm
Sep 21 2017 06:53
I am right now doing exercises to master ORM
My question is
suppose this is the modal

class Category(models.Model):
    name = models.CharField(max_length=50)
    slug = models.SlugField(max_length=50, unique=True)

class Product(models.Model):
    name = models.CharField(max_length=200, unique=True,
                            blank=False, null=False)
    categories = models.ManyToManyField(Category, related_name='products')
Sathish Kumar
@sathisz
Sep 21 2017 06:54
OKay
MilanRgm
@MilanRgm
Sep 21 2017 06:54

from this model, I want to list all the products of category startup then I will do the following query

Category.objects.get(slug=slug).products.all()

this gives me all the product of the category whose slug is whatever passed which if exist
That is queried based on the related_names
what If i have to query without giving related_name
Sathish Kumar
@sathisz
Sep 21 2017 06:56
i think that .all retrieving all the datas
MilanRgm
@MilanRgm
Sep 21 2017 06:56
Category.objects.get(slug=slug).product_set.all() this one yields the Category' object has no attribute 'product_set' error
I want all the products that fall on certain category so I did the first query. Was that wrong?
Sathish Kumar
@sathisz
Sep 21 2017 06:58
Category.objects.filter( try this
MilanRgm
@MilanRgm
Sep 21 2017 06:58
I checked the first query again and it worked
Sathish Kumar
@sathisz
Sep 21 2017 06:58
oh then its cool
Category.objects.filter(slug=slug).products.all()
try this
MilanRgm
@MilanRgm
Sep 21 2017 06:59

I posted 3 products, product1 with category startup, product2 with category startup and product3 with category incubator and when I did

Category.objects.get(slug='startup').products.all()

i got only product1 and product2 which is expected result because product3 does not fall in the startup category

But @sathisz why Category.objects.get(slug=slug).product_set.all() not working?
Sathish Kumar
@sathisz
Sep 21 2017 07:00
I think its not filering
@MilanRgm
MilanRgm
@MilanRgm
Sep 21 2017 07:01

Category.objects.filter(slug='startup').products.all()

this one throws an error

AttributeError: 'QuerySet' object has no attribute 'products' when using filter
Sathish Kumar
@sathisz
Sep 21 2017 07:02
Oh just wait i search for it
MilanRgm
@MilanRgm
Sep 21 2017 07:04
sure
Ruben Das
@rubda
Sep 21 2017 07:59
So, I'm trying to exclude the 'password' field when retrieving the built-in user object class. So basically, User.objects.defer('password'). When returning these objects in JSON the model says "auth.user_deferred_password." which is great. The thing is the fields still contains password. Any ideas?
MilanRgm
@MilanRgm
Sep 21 2017 14:37
Can anyone help me to fix this, please?

Issue is at start each company will have 0 like so when clicked on the like button, it shows 1 Unlike and if again clicked it will show -1 Like. When refresh it will show the right count with right verb. How do I fix this?


class CompanyLikesAPIView(APIView):
    permission_classes = (permissions.IsAuthenticated, )

    def get(self, request, slug=None, format=None):
        company_instance = get_object_or_404(Company, slug=slug)
        company_like = company_instance.likes.all()
        user = request.user
        unlike = False
        liked = False
        if user.is_authenticated():
            if user in company_like:
                company_instance.likes.remove(user)
                unlike = True
                liked = False
            else:
                company_instance.likes.add(user)
                liked = True
        data = {
            'unlike': unlike,
            'liked': liked
        }
        return Response(data)


<script>
        $(document).ready(function(){
          function updateText(btn, newCount, verb){
            btn.text(newCount + " " + verb)
        }
        $(".like-company").click(function(e){
          e.preventDefault()
          var this_ = $(this)
          var likeUrl = this_.attr("data-href")
          var likeCount = parseInt(this_.attr("data-likes")) || 0
          var addLike = likeCount + 1
          var removeLike = likeCount - 1
          if (likeUrl){
          $.ajax({
            url: likeUrl,
            method: "GET",
            data: {},
            success: function(data){
              console.log(data)
              var newLikes;
              if (data.liked){
                updateText(this_, addLike, "Unlike")
              } else {
                updateText(this_, removeLike, "Like")
              }
              }, error: function(error){
                  console.log(error)
                }
            })
          }
        })
        })
</script>

<a href="{{ company.get_like_url }}" class="btn btn-link like-company" data-href="{{ company.get_like_url }}" data-likes="{{ company.likes.count }}"><i class="fa fa-thumbs-o-up"></i>{{ company.likes.count }} like</a>
Luke-VideoLibre
@LukeVideo
Sep 21 2017 15:56
you seem to have gonne mixed up in your updateText.
             if (data.liked){
                updateText(this_, addLike, "Unlike")
              } else {
                updateText(this_, removeLike, "Like")
Oh no thats a toggle logic.. Sorry
Luke-VideoLibre
@LukeVideo
Sep 21 2017 16:44

So i'm facing a new challege. I want to show messages send to to logged user only.
views.py sample

        user = request.user
        message_form = messageForm()
        message_raw_form = messageRawForm()
        user_messages = CluedoMessage.objects.all()   #.filter(receiver = user)
        context = {'message_form':message_form,
                   'message_raw_form' : message_raw_form,
                   'user_messages':user_messages
                   }

template sample

    {% if user_messages %}
      {% for message in user_messages %}
        {{ message }}
      {% endfor %}
    {% endif %}
class CluedoMessage(models.Model):
    receiver = models.ForeignKey(UserInfo)
    message = models.BinaryField(null=True)
    tag = models.BinaryField(null=True)
    nonce = models.BinaryField(null=True)
    sender = models.ForeignKey(User, on_delete=models.CASCADE)
    def sent_to_me(self):
        return self.receiver == user

I tried adding a filter in the view but it failed completely.
it was the commented filter you can see. Has anyone a hint on where i would filter my articles and how.

the error with the filter was

ValueError: Cannot query "testGuy": Must be "UserInfo" instance.

In advance, thanks ! ;)

White Rabbit
@Wahtdb_twitter
Sep 21 2017 18:00
I need someone who has experience with Django channels.