iamteem / redisco

A Python Library for Simple Models and Containers Persisted in Redis
MIT License
301 stars 140 forks source link

Is a ListField of ReferenceFields possible? #26

Open tonycpsu opened 12 years ago

tonycpsu commented 12 years ago

Scenario: I have a "Game" class that I would like to be able to refer to one or more "Player" objects. If I'm okay with storing the player data in each game, I can do:

class Game(models.Model):
    players = models.ListField(Player)

But I'd like it to just be a reference, not contain copies of each player. For one player, this would work:

class Game(models.Model):
    players = models.ReferenceField("Player", related_name="game")

What I really want (or what I think I want) is a ListField of ReferenceFields. Of course, if I try this:

class Game(models.Model):
    players = models.ListField(models.ReferenceField("Player", related_name="game"))

It's not going to work, since it's going to instantiate the ReferenceField instead of it being a class. Is there any way to accomplish what I want?

kiddouk commented 12 years ago

The way I solve that for now is by doing :

class Game(models.Model):
  _players_id = None

  @property 
  def players(self):
    if _players_id is None:
      self._players_id = Set("%s:%s:players_id" % (self.__class__.__name__, self.id)
    return [Player.objects.get_by_id(pid) for pid in self._players_id]

  def add_player(self, player):
    self._players_id.add(player.id)

This is slightly hackish since you have to manually check for not triggering MissingID exception and play so on, But that way, you use a Native Redis object to store your data.

tonycpsu commented 12 years ago

@kiddouk, that is indeed a good workaround -- I do think explicitly supporting ListFields composed of ReferenceFields would be cleaner, but your solution is still pretty clean.

As to how to actually make it work the cleaner way... Python's collections.defaultdict supports this sort of nested structure by allowing you to pass in a callable that creates the class you want to create a defaultdict of. e.g.

defaultdict(int)

for a dict of ints defaulting to 0, or

defaultdict(lambda: defaultdict(int))

Fir a dict of dicts of ints defaulting to 0

Might this approach work in redisco to allow this sort of nesting?

kiddouk commented 12 years ago

That is definitely worth looking at. Do you fancy creating a pull request for testing that out maybe ?

We also have to consider the amount of time lost in making too many requests for get the data. I believe it is a trade of between convenience, object related things and performance.

Let's also have a look at what Ohm is doing (since the project is inspired by Ohm).

tonycpsu commented 12 years ago

I'm afraid my Ruby knowledge is too rudimentary to make head or tail of what Ohm is doing with lists and references. I also just started working with Redis this past weekend, so I'm afraid I might not be the best candidate to give this a go in redisco, either. :/

kiddouk commented 12 years ago

So I got to look at Ohm behavior. It seems that they haven't decided to take the "duplication" approach. We may want to do the same in Redisco. I will mockup something and submit it to see if that could match our expectations.