Coder Social home page Coder Social logo

matchbox-orm's Issues

Default value for Model.ListField does not get set

I have ran into an issue where I set the 'default' param for the subclass of a Model object to be an empty list, however it does not get set when I create a new object of that subclass with no params.

Default values should be filled into the field so that they can be used right away, before new model objects are even saved.

Can't filter on ReferenceField

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

(Feature) Support sub-collections

Currently, collection names are static and root level. I want to make a get query on a sub-collection of a model.

Querying could look something like this:

class A(models.Model):
    a_field_1 = models.TextField()

class B(models.Model):
    b_field_1 = models.TextField()
    b_field_2 = models.TextField()

a = A.objects.get(a_field_1='value1')
b = B.objects.get(parent_model=a, b_field_1='value1', b_field_2='value2')

Internally, the full collection name for 'a' and its own unique id would be appended to the beginning of the collection name for 'b' of class B. Something like this:

full collection name for 'a': a
full collection name for 'b': a/<unique_id_for_a>/b

This format could be used for any depth of sub-collections

.objects.all() skips objects without a field

Hi,
Not sure if this is expected behavior or a bug but figured out I'd ask. I tried to call .objects.all() fetching objects. One of the fields is a TimeStampField, but only some objects have the value assigned to it in the Firebase (it was added later, when already some records were in database).

When calling .objects.all(), all objects without a value for TimeStampField are skipped.

Any idea if I can somehow get all objects, even those that don't have this field?

Dzięki za pomoc!

Inheritance of abstract model

Hello,

I tried an inheritance of abstract model, but it seemed like it failed.

My code is attached below. When executing it, it's only "systemNumber" and "systemName" which have proper values on the Firestore. The fields of "createdAt" and "createdBy" from "Suffix_fsm" don't appear there.

from matchbox import models as fsm, database
from datetime import datetime
database.db_initialization('xxx.json')

class Suffix_fsm(fsm.Model):
    createdAt = fsm.TimeStampField()
    createdBy = fsm.TextField(max_length=30, default='')

    class Meta:
        abstract = True

class SystemMaster(Suffix_fsm):
    systemNumber = fsm.IDField()
    systemName = fsm.TextField(max_length=50, default='')

master = SystemMaster(
    systemNumber=1,
    systemName='name',
    createdAt=datetime.now(),
    createdBy='test',
)
master.save()

It would be great if this issue is resolved.

Thank you !

ReferenceField does not return a proper DocumentReference db value

I noticed that ReferenceField.db_value no longer returns any value, where it previously returned a DocumentReference object.

I have needed to do something like this in order to filter by reference:

class ReferenceField(Field):
...
    def db_value(self, value):
        ...
        return google.cloud.firestore_v1.document.DocumentReference(
            value.collection_name(), value.id, client=db.conn
        )
...

This has allowed me to filter by reference with a full collection name (which I currently hack fix a certain sub-collection)

How can make my model can inherit?

Hi,
in my project, I used similar structures with multiple classes,
such as,

class UserDatabase(models.Model):
    class Meta:
        collection_name = 'User'
    birth = models.TimeStampField()
    email = models.TextField()
    name = models.MapField()

    def __unicode__(self):
        return self.id

    def to_dict(self):
        return self.__dict__

class AnnotatorDatabase(models.Model):
    class Meta:
        collection_name = 'annotators'
    total_annotation = models.IntegerField()

    def __unicode__(self):
        return self.id

    def to_dict(self):
        return self.__dict__

So I want to make above codes to like below.

class BaseDatabase(models.Model):

    def __unicode__(self):
        return self.id

    def to_dict(self):
        return self.__dict__

class Userdatabase(BaseDatabase):
    birth = models.TimeStampField()
    email = models.TextField()
    name = models.MapField()

    class Meta:
        collection_name = 'User'

class AnnotatorDatabase(BaseDatabase):
    total_annotation = models.IntegerField()

    class Meta:
        collection_name = 'Annotator'

but it shows error, AttributeError: Can't inherit from non abstract class so coud you give me any tip for the better design?

Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.