Python Django,选择queryset覆盖值

Python Django,选择queryset覆盖值,python,mysql,django,Python,Mysql,Django,我正在制作一个简单的文字游戏来学习django的过程,问题如下。游戏的特点是房间和门之间,门有一个默认状态,即在玩家互动过程中发生变化 表1车门: class Location_exit(models.Model): exit_from = models.ForeignKey(Location, related_name='exit_from') exit_name = models.CharField(max_length=128) exit_message = mod

我正在制作一个简单的文字游戏来学习django的过程,问题如下。游戏的特点是房间和门之间,门有一个默认状态,即在玩家互动过程中发生变化

表1车门:

class Location_exit(models.Model):
    exit_from = models.ForeignKey(Location, related_name='exit_from')
    exit_name = models.CharField(max_length=128)
    exit_message = models.TextField(blank=True, null=True)
    EXIT_TYPE_CHOICES = (
        (Exit_type.opened.value, 'Open'),
        (Exit_type.closed.value, 'Closed'),
        (Exit_type.hidden.value, 'Hidden'),
    )
    exit_type = models.IntegerField(choices=EXIT_TYPE_CHOICES)
    exit_to=models.ForeignKey(Location, related_name='exit_to')
表2按玩家列出的门状态:

class Location_exit_state(models.Model):
    location_exit = models.ForeignKey(Location_exit)
    user = models.ForeignKey(User)
    exit_type = models.IntegerField(default=0)
这个想法是,当玩家第一次到达出口时,他们会得到默认的打开/关闭/隐藏状态,如果他们对门做了什么,它的新状态将记录在表2中。下次访问时,应从第二个表中加载该值,而关于门的其余(静态)部分数据仍来自表1

有了SQL,我会像

SELECT * FROM Location_exit As a WHERE (id, User) NOT IN Location_exit_state
UNION
SELECT a.exit_from, a.exit_name, a.exit_message, a.exit_to, exit_type FROM Location_exit_state LEFT JOIN Location_exit As a
但是作为python中的一个十足的noob,我不知道在哪里可以找到django的等价物。 有人能帮忙吗?

这是我的方式:

class Location_exit_state(models.Model):
    location_exit = models.ForeignKey(Location_exit, related_name='user_states')
    ...

class Location_exit(models.Model):
    ...
    default_exit_type = models.SmallPositiveIntegerField(...)        

    def exit_type(self, user):
        try:
            return self.user_states.get(user=user).exit_type
        except:
            self.user_states.create(user=user, exit_type=default_exit_type)
            return default_exit_type
    exit_type.alters_data = True

    def set_new_exit_type(self, user, new_type):
        user_exit_state = self.user_states.get(user=user)
        user_exit_state.exit_type = new_type
        user_exit_state.save()
    set_new_exit_type.alters_data = True
这就是我的方式:

class Location_exit_state(models.Model):
    location_exit = models.ForeignKey(Location_exit, related_name='user_states')
    ...

class Location_exit(models.Model):
    ...
    default_exit_type = models.SmallPositiveIntegerField(...)        

    def exit_type(self, user):
        try:
            return self.user_states.get(user=user).exit_type
        except:
            self.user_states.create(user=user, exit_type=default_exit_type)
            return default_exit_type
    exit_type.alters_data = True

    def set_new_exit_type(self, user, new_type):
        user_exit_state = self.user_states.get(user=user)
        user_exit_state.exit_type = new_type
        user_exit_state.save()
    set_new_exit_type.alters_data = True

为什么要从表2中加载门的状态?为什么不在他们自己的模型中更新门的状态?@zarch可能是为了为不同的玩家保存不同的退出状态?!@Zarcch我希望游戏一次可以被多个玩家使用,每个玩家都有自己的变量集,记录他们个人的游戏状态。doors基本上是一个静态元素,因此为每个用户制作doors表的完整副本是没有意义的。任务是从主表中加载尽可能多的公共数据,并根据需要替换玩家游戏状态中的少数值。@schwobasegll完全正确,先生。您的位置退出模型中不需要字段退出类型。这似乎是多余的。只需添加一个函数,查询给定用户的正确位置\退出\状态实例并从中获取值。为什么要从表2中加载门的状态?为什么不在他们自己的模型中更新门的状态?@zarch可能是为了为不同的玩家保存不同的退出状态?!@Zarcch我希望游戏一次可以被多个玩家使用,每个玩家都有自己的变量集,记录他们个人的游戏状态。doors基本上是一个静态元素,因此为每个用户制作doors表的完整副本是没有意义的。任务是从主表中加载尽可能多的公共数据,并根据需要替换玩家游戏状态中的少数值。@schwobasegll完全正确,先生。您的位置退出模型中不需要字段退出类型。这似乎是多余的。只需添加一个函数,查询给定用户的正确位置\退出\状态实例,并从中获取值。非常感谢,我明白了这个想法。应该是完成任务的有效方法。“我会尝试一下,然后将结果返回给你。我能够利用你给出的建议制作出一个很好的工作模型,@schwobasegll。”。再次感谢。非常感谢,我明白了。应该是完成任务的有效方法。“我会尝试一下,然后将结果返回给你。我能够利用你给出的建议制作出一个很好的工作模型,@schwobasegll。”。再次感谢。