Guest User

Untitled

a guest
Jan 16th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. class AllInManager(models.Manager):
  2. """
  3. Provides the method from_related_ids, which lets you
  4. select some objects by providing a list of related ids
  5. (The huge difference to __in is that the objects have
  6. to match al of the ids, not only one)
  7. Model Example::
  8.  
  9. class Article(models.Model):
  10. text = models.TextField()
  11. tags = ManyToManyField('Tag')
  12. objects = AllInManager()
  13.  
  14. Usage::
  15.  
  16. Article.objects.from_related_ids((1,2,3,4), 'tags')
  17. """
  18. def from_related_ids(self, id_list, field):
  19. mapper = {'%s__in' % field: id_list}
  20. query = self.model.objects.filter(**mapper)
  21. if len(id_list) > 1:
  22. query = query.annotate(c=models.Count(field)).filter(c=len(id_list))
  23. return query
  24.  
  25. class Ability(models.Model):
  26. """ Player ability model """
  27.  
  28. ABILITY_TYPE_CHOICES = (
  29. ('1', 'attack'),
  30. ('2', 'defense'),
  31. ('3', 'science'),
  32. )
  33.  
  34. prerequisites = models.ManyToManyField('self', related_name='required', symmetrical=False, blank=True)
  35. name = models.CharField(max_length=255, unique=True)
  36. slug = models.SlugField(max_length=255)
  37. ability_type = models.CharField(max_length=3, choices=ABILITY_TYPE_CHOICES)
  38. research_time = models.IntegerField()
  39. objects = AllInManager()
  40. class Meta:
  41. verbose_name_plural = "Abilities"
  42.  
  43. def __unicode__(self):
  44. return self.name
  45.  
  46. def get_absolute_url(self):
  47. return ('ability_item_view', (), {'slug': self.slug})
  48.  
  49. class Ship(models.Model):
  50. """
  51. Player ship model used for storing player ship data and log in credentials
  52. """
  53.  
  54. name = models.CharField(max_length=255, unique=True)
  55.  
  56. class Meta:
  57. verbose_name_plural = "Ships"
  58. ordering = ['-name']
  59.  
  60. def __unicode__(self):
  61. return u'%s' % (self.name)
  62.  
  63. class Researched(models.Model):
  64. """
  65. Player's researched abilites
  66. """
  67.  
  68. ship = models.ForeignKey(Ship)
  69. ability = models.ForeignKey(Ability)
  70.  
  71. class Meta:
  72. verbose_name_plural = "Researched Items"
  73.  
  74. def __unicode__(self):
  75. return u'%s' % (self.ability.name)
  76.  
  77. Ok assuming i have a player IE ship i can get the Resarched objects what i am trying to do is query the abilities model and exclude any abilites the user cant research yet as they dont have all the prerequisites required for that ability. as you can see prerequisites is a non symetrical m2m relationship on the ability model (self)
  78.  
  79. so for example, if there are two abilities in the database Shield Lvl 1 and shields LVL 2, shields lvl 2 requires the player has researched shields lvl 1. so if the player has no prior reserached abilites the query im trying to figure out would only return a shield level 1 ability object
Add Comment
Please, Sign In to add comment