Listen to the error message.  Include the primary key in the queryset.  You can 
use any field and alias it as “id” if you need to trick the system.
Although, couldn’t that raw query use the ORM in a better way?
Post.objects.annotate(uno=Count(‘title’), 
published_date_date=Cast(‘published_date’, DateField())).values(‘uno’, 
‘published_date_date’)

From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On 
Behalf Of soumyajit banerjee
Sent: Wednesday, September 26, 2018 7:17 AM
To: django-users@googlegroups.com
Subject: django raw sql query does not return data

Hi everyone,


class Post(models.Model):

    author = models.ForeignKey('auth.User',on_delete=models.CASCADE,)

    title = models.CharField(max_length=200)

    text = RichTextField()

    created_date = models.DateTimeField(default=timezone.now)

    published_date = models.DateTimeField(blank=True, null=True)



    def __str__(self):

        return self.title

this is my sample model

and this is my view

def index(request):

    fetch_post_data = 
Post.objects.filter(published_date__lte=timezone.now()).order_by('-created_date')[:5]

    fetch_pgraph_data = Post.objects.raw("select count(title) as 
uno,published_date from blog_post group by DATE(published_date)")

    #fetch_pgraph_data = Post.objects.raw("select * from blog_post")

    template_var = {'postodj':fetch_post_data,'userdataobj':fetch_pgraph_data}

    return render(request,'home.html',context=template_var)

when I am trying to fetch the data using fetch_pgraph_data, it's showing Raw 
query must include the primary key


--
Soumyajit Banerjee

[http://cdn2.hubspot.net/hubfs/184235/dev_images/signature_app/facebook_sig.png]<https://www.facebook.com/recklessLYF>
  
[http://cdn2.hubspot.net/hubfs/184235/dev_images/signature_app/linkedin_sig.png]
 <https://www.linkedin.com/in/soumyajit-banerjee-11992b25/>



--
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
django-users+unsubscr...@googlegroups.com<mailto:django-users+unsubscr...@googlegroups.com>.
To post to this group, send email to 
django-users@googlegroups.com<mailto:django-users@googlegroups.com>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAG8c6EFsFt_1UwgSch_1mY92x%2BboSrwow7H5umj4-WeYs0f2hg%40mail.gmail.com<https://groups.google.com/d/msgid/django-users/CAG8c6EFsFt_1UwgSch_1mY92x%2BboSrwow7H5umj4-WeYs0f2hg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/fa8b3342e418433c9768db663266ebef%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.

Reply via email to