gameboy86 / matchbox-orm

ORM package for google Cloud Firestore.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't filter on ReferenceField

Whytey opened this issue · comments

I have an 'Attempt' Model that includes a ReferenceField to 'Challenge' Model. When I attempt to filter Attempt based on Challenge, I get an error, that appears to be coming from the Firestore library.

Part of Stack Trace:

File "/home/djwhyte/workspace/BBBBCSegmentTracker/main.py", line 47, in attempts
    atts = Attempt.objects.filter(challenge=c).get()
  File "/home/djwhyte/workspace/BBBBCSegmentTracker/venv/lib/python3.6/site-packages/matchbox/queries/queries.py", line 113, in get
    res = list(self.execute())
  File "/home/djwhyte/workspace/BBBBCSegmentTracker/venv/lib/python3.6/site-packages/matchbox/queries/queries.py", line 100, in execute
    for d in self.raw_execute() if d
  File "/home/djwhyte/workspace/BBBBCSegmentTracker/venv/lib/python3.6/site-packages/matchbox/queries/queries.py", line 84, in raw_execute
    return self.make_query().stream()
  File "/home/djwhyte/workspace/BBBBCSegmentTracker/venv/lib/python3.6/site-packages/matchbox/queries/queries.py", line 70, in make_query
    bsq = bsq.where(*w)
  File "/home/djwhyte/workspace/BBBBCSegmentTracker/venv/lib/python3.6/site-packages/google/cloud/firestore_v1/collection.py", line 250, in where
    return query.where(field_path, op_string, value)
  File "/home/djwhyte/workspace/BBBBCSegmentTracker/venv/lib/python3.6/site-packages/google/cloud/firestore_v1/query.py", line 272, in where
    value=_helpers.encode_value(value),
  File "/home/djwhyte/workspace/BBBBCSegmentTracker/venv/lib/python3.6/site-packages/google/cloud/firestore_v1/_helpers.py", line 200, in encode_value
    "Cannot convert to a Firestore Value", value, "Invalid type", type(value)
TypeError: ('Cannot convert to a Firestore Value', <Challenge: 9zcSTiUbeOe5bNjqXV3d>, 'Invalid type', <class 'model.Challenge'>)

The pertinent area of my Models:

class BaseModel(models.Model):
    audit_inserted = models.TimeStampField()

    class Meta:
        abstract = True

    def __unicode__(self):
        return self.id

    @abstractmethod
    def add(self):
        pass


class Challenge(BaseModel):
    date_from = models.TimeStampField()
    date_to = models.TimeStampField()
    segment_id = models.IntegerField()
    segment_name = models.TextField()
    audit_inserted = models.TimeStampField(datetime.now())

    @staticmethod
    def add(segment, year, month):
        """Takes a stravalib.model.Segment object and adds it as the target challenege to cover the given month of the given year"""
        _, end_day = calendar.monthrange(year, month)

        date_from = datetime.combine(date(year, month, 1), datetime.min.time())
        date_to = datetime.combine(date(year, month, end_day), datetime.min.time())

        c = Challenge.objects.create(segment_id=segment.id, segment_name=segment.name, date_from=date_from,
                                     date_to=date_to, audit_inserted=datetime.utcnow())
        c.save()

        return c


class Attempt(BaseModel):
    member = models.ReferenceField(Member)
    challenge = models.ReferenceField(Challenge)
    recorded_time_secs = models.IntegerField()
    activity_timestamp = models.TimeStampField()
    activity_id = models.IntegerField(blank=True)

    @staticmethod
    def add(effort, member, challenge):
        a = Attempt.objects.create(id=effort.id, member=member, challenge=challenge,
                                   recorded_time_secs=effort.elapsed_time.total_seconds(),
                                   activity_timestamp=effort.start_date_local, activity_id=effort.activity.id,
                                   audit_inserted=datetime.utcnow())
        a.save
                     member_id=member.id, challenge_id=challenge.id, attempt_id=a.id)

        return a

The code that is making the call:

        c = Challenge.objects.get(id=challenge)
        print (c)
        atts = Attempt.objects.filter(challenge=c).get()

Note that the 'print (c)' above is correctly outputting the Challenge object that is correctly referenced: <Challenge: 9zcSTiUbeOe5bNjqXV3d>

Notable parts of requirements.txt:

google-cloud-firestore==1.4.0
matchbox-orm==0.2

Hi,
thanks, for your issue. I published version 0.2.1 which should fixed problem. Please give me know, when you check, if everything is ok