Reputation: 31
I have a Battleship-like grid (with positions A1, B1, C1, ... A2, B2, C2, ...) and objects that are moving around on the grid, where a position can only be occupied by one object at any given time. I'm trying to use Django models to store the paths taken by each object and was wondering the best way to do that.
For example, I would want to be able to query and see that ObjectZ has been to [A1, B1, B2, C2, C3, D3, D2, C2, C3] - then I would know that ObjectZ started at A1 and is now at C3.
I tried to use the ManyToManyField, but soon realized that I could not add the same object multiple times (meaning, I could not add C2 more than once).
Thanks in advance!
Upvotes: 0
Views: 68
Reputation: 109
Your solution here could be to create your own class that would link those two and based on the battleship you can have an order field that you increment each time a new movement for a ship is created so in that way you can keep track of the order.
class Movements(models.Model):
order = models.IntegerField()
battleship = models.ForeignKey(Battleship, on_delete=models.CASCADE, related_name="movements")
square = models.ForeignKey(Square, on_delete=models.CASCADE, related_name="movements")
Upvotes: 1