PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 19, 2022

[FIXED] how to create a SimpleListFilter in django

 October 19, 2022     admin, django, filter     No comments   

Issue

I don't succeed to write a query filter. I have 3 models: Patient, Prescription and User

I write you only what is relevant for my question

Patient:

class Patient(models.Model):
   user = models.OneToOneField(
   settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

Prescription:

class Prescription(models.Model):
   user = models.ForeignKey(
          User,
          null=True,
          blank=False,
          on_delete=models.DO_NOTHING
         )
         file_extention = models.CharField(
         'file extention',
         max_length=8,
         null=True,
         blank=True,
         )

So the relation between both of models (Patient and Prescription) are through User.

in the PatientAdmin, I want to filter on the file_extension according pdf or jpg of the prescription uploaded. I created a SimpleListFilter but impossible to find the right query.

class PrescriptionFileExtensionFilter(SimpleListFilter):
    """This filter is being used in django admin panel in 
    patient model."""
    title = 'Prescription File Ext'
    parameter_name = 'file_extention'

    def lookups(self, request, model_admin):
       return (
               ('pdf', 'PDF'),
               ('jpg', 'JPG'),
              )

    def queryset(self, request, queryset):
       for user in queryset:
           if self.value() == 'pdf':
              return queryset.filter(user=user.user).filter
                     (prescription__file_extention="pdf")

           if self.value() == 'jpg':
              return queryset.filter(user=user.user).filter 
                     (prescription__file_extention="jpg")

That's not working... Do I need the for user in queryset: need What could be the query to bring me all the users with a prescription with file_extension = "pdf" (or "jpg")


Solution

You are trying to get a key from the prescription object in print(mydict['file_extention']) which I believe is causing the issue - you would instead access that property via mydict.file_extention - though I should add that mydict is not an accurate variable name for a model object as it isn't a dictionary. I don't think that for loop is actually doing anything other than printing a particular value so it can be removed altogether.

As an aside, you have two filters on your queryset, this can just be expressed as a single filter, separate by a comma, e.g.

return queryset.filter(user=user.user, prescription__file_extention="pdf")

You are also calling user.user, presumably you just want to get the user model which is kept in request.user - is that what your for loop was trying to do?

Edit

If you want to get all of the users but just filtered by JPG or PDF then you need to remove two things:

  1. The for-loop of for user in queryset
  2. The filter of .filter(user=user.user)

The for loop is unnecessary in the queryset function and the filter is just getting a single user, but you want to get all of them - correct?



Answered By - 0sVoid
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing