Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Django jquery

Before in my models.py I had

report_division = models.TextField(blank=True, max_length=40)

and I counted by filter using this line in my views.py:

CRS = Post.objects.filter(report_division='Something').count()

Now I have a seperate class in my models.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Divizija(models.Model):
    naziv_divizija = models.CharField(max_length=40)

    def __str__(self):
        return self.naziv_divizija

class Post(models.Model):
     report_division = models.ForeignKey(Divizija, on_delete=models.SET_NULL, null=True, 
     verbose_name="Divizija")

I can’t get my query to work now. I tried:

CRS = Post.objects.filter(report_division=1).count()
CRS = Post.objects.filter(report_division_id=1).count()
CRS = Post.objects.filter(report_division='Something').count()

>Solution :

You have to specify the field of the foreign key you want to filter by

CRS = Post.objects.filter(report_division__naziv_divizija=1).count()
CRS = Post.objects.filter(report_division_id=1).count()  # this one is right
CRS = Post.objects.filter(report_division__naziv_divizija='Something').count()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading