S
S
Sanya Hihi Haha2020-05-06 17:07:47
Django
Sanya Hihi Haha, 2020-05-06 17:07:47

How to organize links in a database?

There is such a model

# Эта модель хранит всех героев , в том числе и активных, но с меткой hero_active = True
class UserAllHero(models.Model):
    user_hero = models.ForeignKey(User, on_delete=models.CASCADE)
    #hero_equip = models.OneToOneField(HeroInventory,on_delete=models.CASCADE)

    hero_name = models.CharField(default='Герой', max_length=32)
    hero_image = models.ImageField(upload_to='hero_img', default='')
    hero_life = models.IntegerField(default=20)
    hero_armour = models.IntegerField(default=20)
    hero_damage = models.IntegerField(default=120)
    hero_xp = models.IntegerField(default=0)
    hero_level = models.IntegerField(default=1)
    hero_unique_id = models.CharField(default='unique_id', max_length=48)
    hero_class = models.CharField(default='Класс', max_length=32)
    hero_badge = models.ImageField(upload_to='badge',default='')
    hero_power = models.IntegerField(default=25)
    hero_points = models.IntegerField(default=10)

    life_points = models.IntegerField(default=0)
    armour_points = models.IntegerField(default=0)
    damage_points = models.IntegerField(default=0)
    hero_active = models.BooleanField(default=False)

    weapon_one = models.OneToOneField('UserInventory.UserInvWeapon', on_delete=models.CASCADE, related_name='weapon1',blank=True,null=True)
    weapon_two = models.OneToOneField('UserInventory.UserInvWeapon', on_delete=models.CASCADE, related_name='weapon2',blank=True,null=True)
    helmet = models.OneToOneField('UserInventory.UserInvHelmet', on_delete=models.CASCADE, related_name='helm',blank=True,null=True)
    cuirass = models.OneToOneField('UserInventory.UserInvCuirass', on_delete=models.CASCADE, related_name='chest',blank=True,null=True)
    pants = models.OneToOneField('UserInventory.UserInvPants', on_delete=models.CASCADE, related_name='pant',blank=True,null=True)
    gloves = models.OneToOneField('UserInventory.UserInvGloves', on_delete=models.CASCADE, related_name='gloves',blank=True,null=True)
    boots = models.OneToOneField('UserInventory.UserInvBoots', on_delete=models.CASCADE, related_name='boot',blank=True,null=True)

    def __str__(self):
        return self.user_hero.username


It stores the user's heroes, as well as links with items from other tables (those in this case are typical inventory as in rpg games)

, here is a model with weapons
class UserInvWeapon(models.Model):
    user_weapon = models.ForeignKey(User, on_delete=models.CASCADE)
    

    user_weapon_name = models.CharField(default='Меч', max_length=60)
    user_weapon_level = models.IntegerField(default=1)
    user_weapon_damage = models.IntegerField(default=50)
    user_weapon_inc_damage = models.IntegerField(default=10)
    user_weapon_rarity = models.CharField(default='common', max_length=32)
    user_weapon_img = models.ImageField(default='shop_sword', upload_to='shop_sword')
    user_weapon_status = models.BooleanField(default=False)
    user_weapon_unique_id = models.CharField(default='un_id', max_length=64,primary_key=True)
    user_weapon_class = models.CharField(default='класс оружия',max_length=64)


Actually a question, - How to me it is correct to organize communications in this case? so that each hero can only equip 1 weapon from the table, and if a weapon is worn by any hero, do not give the opportunity to put on another hero.
And in the future, on the inventory page, do not display items associated with any hero.
It seems like OneToOne itself limits such possibilities, but apparently I need something like reverse-relationship in order to find already associated with some hero in the UserInvWeapon

model Thank you

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question