Skip to content
This repository has been archived by the owner on Jul 20, 2018. It is now read-only.

Is a ListField of ReferenceFields possible? #26

Open
tonycpsu opened this issue Mar 5, 2012 · 5 comments
Open

Is a ListField of ReferenceFields possible? #26

tonycpsu opened this issue Mar 5, 2012 · 5 comments

Comments

@tonycpsu
Copy link

tonycpsu commented Mar 5, 2012

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
Copy link

kiddouk commented Mar 5, 2012

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
Copy link
Author

tonycpsu commented Mar 5, 2012

@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
Copy link

kiddouk commented Mar 6, 2012

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
Copy link
Author

tonycpsu commented Mar 6, 2012

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
Copy link

kiddouk commented Mar 12, 2012

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants