#Filter non model datetime field

22 messages · Page 1 of 1 (latest)

drifting pelican
#

What do you mean your field is non model?

earnest epoch
#

as I have added that field later by serializers.SerializerMethodField() hence I'm getting that in get api response. But that field was not in my django models. As default filtering of lte, lt, gte, gt, exact only works on fields which are present in models so how to filter based on this extra field

#

my model is:

class Candidate(models.Model):
    In_HOUSE = "in_house"
    MIGRATED = "migrated"
    SOURCE_CHOICES = (
        (MIGRATED, "Migrated"),
        (In_HOUSE, "In House")
    )

    email = models.EmailField(null=True, blank=True)
    source = models.CharField(max_length=10, choices=SOURCE_CHOICES, default=In_HOUSE, blank=True)
    
    # reminders related fields
    resume_pending_reminder_active = models.BooleanField(default=False)
    audio_pending_reminder_active = models.BooleanField(default=False)
    grammar_pending_reminder_active = models.BooleanField(default=False)
    last_contacted_at = models.DateTimeField(blank=True, null=True)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    # user = models.OneToOneField(User, on_delete=models.CASCADE, null= True, blank= True)

    def __str__(self):
        return f"{self.email}"
#

my serializer is:

class FormDashboardSerializer(serializers.ModelSerializer):
    candidate = serializers.SerializerMethodField()
    email = serializers.SerializerMethodField()
    responses = CandidateResponseSerializer(many=True, source="filtered_responses")
    latest_submission = serializers.SerializerMethodField()
    # comm_score = serializers.SerializerMethodField()
    # noisy_audio = serializers.SerializerMethodField()

    class Meta:
        model = Candidate
        fields = ('candidate', 'email', 'responses', 'created_at', 'updated_at',
                  'latest_submission') # , 'comm_score', 'noisy_audio')
    
    def get_candidate(self, obj):
        return obj.id

    def get_email(self, obj):
        return obj.email
    
    def get_latest_submission(self, obj):
        return obj.latest_submission
#

now I want to apply filters like gte gt lte lt exact on "latest_submission"

drifting pelican
#

Those are fields on the model, so you should be able to use those lookups.

#

And I suspect you don't need the SerializerMethodFields. You could mark those fields as read only on the meta class.

earnest epoch
#

this is my get api response.

earnest epoch
drifting pelican
#

Ok, that's fine.

#

What have you tried so far?

earnest epoch
#

Nothing significant, confused about how to write custom filter for this.

#

I have parent query set

   def qs(self):
        parent_qs = super().qs
        kwargs = dict(self.data)
        form_id = int(kwargs.get("form_id")[0]) if kwargs.get("form_id") else None

        candidate_responses = CandidateResponse.objects.all()
        if form_id:
            candidate_responses = candidate_responses.filter(field__fieldset__form_id=form_id)

        filters = Q()
for key, value in kwargs.items():
            if key == 'latest_submission':
                print(parent_qs)
```I need to configure this```
                continue
drifting pelican
#

Have you looked at django-filters?

#

Otherwise, break the problem down.

  1. how do you send a value to the server?
  2. how do you filter a QuerySet?
  3. how do you wire those together?
earnest epoch
#

Yes I tried looking at default file of filters provided by django.
Can you give a hint for those 3 points?

#

I tried looking on stackoverflow but seems like I am not using right keywords or what because there is no problem which is closer to mine

drifting pelican
earnest epoch
#

alright

drifting pelican
#

If you're unfamiliar with forms and the ORM, django is going to be a tough slog. Learning the basics of those will help you along, especially with this task.

#

And it seems like this may be for school or you're en entry level dev. In either case, I think this is a good learning opportunity for you.

earnest epoch
#

gotcha. Thank you! I'll try to look into that. Otherwise I always have the option to write gte gt lte lt filters for my query from scratch