Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 动态更新计数查询django ORM?_Python_Django_Postgresql - Fatal编程技术网

Python 动态更新计数查询django ORM?

Python 动态更新计数查询django ORM?,python,django,postgresql,Python,Django,Postgresql,我在django开发了一个网站,有两种型号:Match,Month 我想在匹配模型中更新一个查询,这样它也会在月模型中更新一个特定的查询 型号。py: 来自django.db导入模型的 班级月份(models.Model): name=models.CharField(最大长度=20) 匹配\u count=models.IntegerField() 类匹配(models.Model): 第一组=models.CharField(最大长度=20) 第二组=models.CharField(最大长

我在django开发了一个网站,有两种型号:Match,Month

我想在匹配模型中更新一个查询,这样它也会在月模型中更新一个特定的查询

型号。py:

来自django.db导入模型的

班级月份(models.Model):
name=models.CharField(最大长度=20)
匹配\u count=models.IntegerField()
类匹配(models.Model):
第一组=models.CharField(最大长度=20)
第二组=models.CharField(最大长度=20)
月份=models.CharField(最大长度=20)
我在我的views.py文件中进行了这些查询:

_query_one = Month(name = 'January',matches_count = 0)
_query_one.save()

_query_two = Month(name = 'February',matches_count = 0)
_query_two.save()

_query_three = Match(first_team = 'LA',second_team = 'NYC', in_month = 'March')
_query_three.save()

_query_four = Match(first_team = 'SF',second_team = 'LV', in_month = 'February')
_query_four.save()
我的目标是将2月份的新“matches_count”值从0更新为1

我已经寻找了一种更聪明的方法来实现这一点,除了在查询之后立即运行此代码段:

\u query\u to\u update=Month.objects.get(name='二月')
_查询到更新。匹配计数=1
_查询到更新。更新()
我认为django中有一种特殊类型的字段用于动态更新。 或者,要运行的特定代码,用于验证模型是否匹配以及是否存在依赖项

我还认为这是任何django开发人员和im计划在我的站点中拥有更多相互依赖的模型的共同需求

如果相关的话,我会在我的项目中使用PostGreSQL

任何帮助都将不胜感激。

请不要保留
月份
型号中的匹配数。您在这里介绍了数据复制,结果证明,即使在同一个数据库上,保持数据同步也是一个比看起来更困难的问题

我建议您在
匹配
模型中添加一个。这保证了引用的完整性:数据库将确保它指向有效的
Month
对象。此外,您还可以使用
Month
对象,这样就增加了一个“语义层”,代码不再具有语义层

由该查询集生成的
Month
对象将具有一个额外属性
.matches\u count
,该属性包含相关
Match
对象的数量。如果您经常需要,可以在
对象
管理器中对此进行注释:

class MonthManager(models.Manager):
    def get_queryset(self):
        super().get_queryset().annotate(
            matches_count=Count('match')
        )

class Month(models.Model):
    name = models.CharField(max_length = 20)
    objects = MonthManager()
class MonthManager(models.Manager):
def get_queryset(自我):
super().get_queryset().annotate(
匹配项\计数=计数('匹配')
)
班级月份(models.Model):
name=models.CharField(最大长度=20)

objects=MonthManager()
请不要这样做。这里引入了大量重复数据。事实证明,即使在同一个数据库上同步数据,也是一个比人们预期的更复杂的问题。
from django.db.models import Count

Month.objects.annotate(
    matches_count=Count('match')
)
class MonthManager(models.Manager):
    def get_queryset(self):
        super().get_queryset().annotate(
            matches_count=Count('match')
        )

class Month(models.Model):
    name = models.CharField(max_length = 20)
    objects = MonthManager()