热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在这种情况下,什么是适当的模型来减少逻辑?-Whatisthepropermodeltoreducelogicinthissituation?

Iamsettingupamodelwheretwoplayersareinvolvedinacompetition.Imleaningtowardsthismo

I am setting up a model where two players are involved in a competition. I'm leaning towards this model:

我正在建立一个模型,在这个模型中有两个参与者参与竞争。我倾向于这种模式:

def match(models.Model):
    player = ForeignKey(Player)
    oppOnent= ForeignKey(Player)
    score = PositiveSmallIntegerField()
    games_won = PositiveSmallIntegerField()
    games_lost = PositiveSmallIntegerField()
    won_match = BooleanField()

There are statistics involved, however, and it would require another pull to find the matching record for the opponent if I want to describe the match in full.

不过,这里涉及到一些统计数据,如果我想完整地描述这场比赛,就需要再次拉拽才能找到对手的匹配记录。

Alternatively I could set up the model to include full stats:

或者,我可以建立模型,包括完整的统计数据:

def match(models.Model):
    home_player = ForeignKey(Player)
    away_player = ForeignKey(Player)
    home_player_score = PositiveSmallIntegerField()
    away_player_score = PositiveSmallIntegerField()
    ...

But that seems equally bad, as I would have to do two logic sets for one player (to find his scores when he's home_player and his scores when he's away_player).

但这似乎同样糟糕,因为我必须为一个玩家设置两个逻辑集(在他在家的时候查找他的分数,在他不在的时候查找他的分数)。

The final option is to do two inserts per match, both with full stats, and keep redundant data in the table.

最后一个选项是每次匹配做两个插入,都有完整的统计数据,并在表中保留冗余数据。

There seems like a better way, and therefore I poll SO.

似乎有更好的方法,所以我投票了。

3 个解决方案

#1


1  

Id go with the first model and use select_related() to avoid the extra db calls.

Id使用第一个模型,并使用select_related()避免额外的db调用。

#2


1  

If you're looking to reduce redundancy and maintain consistiency of logic...

如果你想减少冗余并保持逻辑的一致性……

Match: - id
- name

匹配:- id - name

Match_Player: (2 records per match)
- match_id
- player_id
- is_home

Match_Player:(每场2条记录)- match_id - player_id - is_home

Match_Player_Score:
- match_id
- player_id
- score

Match_Player_Score: - match_id - player_id - score

#3


0  

I'd avoid having redundant data in the database. This leaves open the possibility of the database data getting internally inconsistent and messing up everything.

我将避免数据库中存在冗余数据。这会导致数据库数据在内部变得不一致并导致一切混乱。

Use a single entry per match, as in your second example. If you plan ahead, you can accomplish the two sets of logic pretty easily. Take a look at proxy models. There might be an elegant way to do this -- have all of your logic refer to the data fields through accessors like get_my_score and get_opponent_score. Then build a Proxy Model class which swaps home and away.

每个匹配使用一个条目,如第二个示例中所示。如果你提前计划,你可以很容易地完成这两套逻辑。看看代理模型。有一种很好的方法可以做到这一点——让您的所有逻辑通过诸如get_my_score和get_defaulent_score这样的访问器引用数据字段。然后,构建一个代理模型类,该类交换了home和away。

class match(models.Model):

    def get_my_score(self):
        return self.home_player_score

    def get_opponent_score(self):
        return self.away_player_score

    def did_i_win(self):
        return self.get_my_score() > self.get_opponent_score()


class home_player_match(match):
    class Meta:
        proxy = True

    def get_my_score(self):
        return self.away_player_score

    def get_opponent_score(self):
        return self.home_player_score

Or maybe you want two Proxy models, and have the names in the base model class be neutral. The problem with this approach is that I don't know how to convert a class from one proxy model to another without reloading from the database. You want a "rebless" as in perl. You could do this by using containment rather than inheritance. Or maybe just a flag in the wrapper class (not stored in the database) saying whether or not to swap fields. But I'd recommend some solution like that -- solve the tricky stuff in code and don't let the database get inconsistent.

或者,您可能需要两个代理模型,并让基模型类中的名称保持中立。这种方法的问题是,我不知道如何在不重新加载数据库的情况下将一个类从一个代理模型转换到另一个代理模型。您需要像在perl中那样使用“rebless”。您可以通过使用容器而不是继承来实现这一点。或者可能只是包装器类中的一个标志(不是存储在数据库中),说明是否交换字段。但我还是建议一些类似的解决方案——解决代码中的棘手问题,不要让数据库变得不一致。


推荐阅读
  • Commit1ced2a7433ea8937a1b260ea65d708f32ca7c95eintroduceda+Clonetraitboundtom ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了游标的使用方法,并以一个水果供应商数据库为例进行了说明。首先创建了一个名为fruits的表,包含了水果的id、供应商id、名称和价格等字段。然后使用游标查询了水果的名称和价格,并将结果输出。最后对游标进行了关闭操作。通过本文可以了解到游标在数据库操作中的应用。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 本文讨论了在数据库打开和关闭状态下,重新命名或移动数据文件和日志文件的情况。针对性能和维护原因,需要将数据库文件移动到不同的磁盘上或重新分配到新的磁盘上的情况,以及在操作系统级别移动或重命名数据文件但未在数据库层进行重命名导致报错的情况。通过三个方面进行讨论。 ... [详细]
  • 006_Redis的List数据类型
    1.List类型是一个链表结构的集合,主要功能有push,pop,获取元素等。List类型是一个双端链表的结构,我们可以通过相关操作进行集合的头部或者尾部添加删除元素,List的设 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • 本文介绍了一个题目的解法,通过二分答案来解决问题,但困难在于如何进行检查。文章提供了一种逃逸方式,通过移动最慢的宿管来锁门时跑到更居中的位置,从而使所有合格的寝室都居中。文章还提到可以分开判断两边的情况,并使用前缀和的方式来求出在任意时刻能够到达宿管即将锁门的寝室的人数。最后,文章提到可以改成O(n)的直接枚举来解决问题。 ... [详细]
  • 3.223.28周学习总结中的贪心作业收获及困惑
    本文是对3.223.28周学习总结中的贪心作业进行总结,作者在解题过程中参考了他人的代码,但前提是要先理解题目并有解题思路。作者分享了自己在贪心作业中的收获,同时提到了一道让他困惑的题目,即input details部分引发的疑惑。 ... [详细]
  • 本文介绍了C++中的引用运算符及其应用。引用运算符是一种将变量定义为另一个变量的引用变量的方式,在改变其中一个变量时,两者均会同步变化。引用变量来源于数学,在计算机语言中用于储存计算结果或表示值抽象概念。变量可以通过变量名访问,在指令式语言中引用变量通常是可变的,但在纯函数式语言中可能是不可变的。本文还介绍了引用变量的示例及验证,以及引用变量在函数形参中的应用。当定义的函数使用引用型形参时,函数调用时形参的改变会同时带来实参的改变。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • Python SQLAlchemy库的使用方法详解
    本文详细介绍了Python中使用SQLAlchemy库的方法。首先对SQLAlchemy进行了简介,包括其定义、适用的数据库类型等。然后讨论了SQLAlchemy提供的两种主要使用模式,即SQL表达式语言和ORM。针对不同的需求,给出了选择哪种模式的建议。最后,介绍了连接数据库的方法,包括创建SQLAlchemy引擎和执行SQL语句的接口。 ... [详细]
  • 在Oracle11g以前版本中的的DataGuard物理备用数据库,可以以只读的方式打开数据库,但此时MediaRecovery利用日志进行数据同步的过 ... [详细]
author-avatar
国芝翰娥264
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有