社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

在django模型中,如何用“timestamp”作为主键覆盖“id”作为主键?

connor • 6 年前 • 2171 次点击  

我正在构建一个Django ETL引擎,该引擎使用企业API从GitHub中提取数据,以收集有关内部公司协作的指标。我设计了一些模式,我现在意识到由于ORM自动设置的pk(主键),该模式无法扩展。提取的主要特征之一是 id 创建存储库、对日志发表评论等的人。

我最初的想法是让ORM自动设置 身份证件 作为 PK 但这不起作用,因为GET请求将每周运行一次,并且它将引发错误,导致覆盖 ID 主键失败。

我做了一些研究,一个潜在的解决方案是创建一个在这里引用的元类: Django model primary key as a pair

但我不确定创建几个元类是否会从一开始就击败元类的全部要点。

这是我为其设置的架构 models.py

from django.db import models
from datetime import datetime

""" Contruction of tables in MySQL instance """


class Repository(models.Model):
    id = models.PositiveIntegerField(null=False, primary_key=True)
    repo_name = models.CharField(max_length=50)
    creation_date = models.CharField(max_length=21, null=True)
    last_updated = models.CharField(max_length=30, null=True)
    qty_watchers = models.PositiveIntegerField(null=True)
    qty_forks = models.PositiveIntegerField(null=True)
    qty_issues = models.PositiveIntegerField(null=True)
    main_language = models.CharField(max_length=30, null=True)
    repo_size = models.PositiveIntegerField(null=True)
    timestamp = models.DateTimeField(auto_now=True)

class Contributor(models.Model):
    id = models.IntegerField(null=False, primary_key=True)
    contributor_cec = models.CharField(max_length=30, null=True)
    contribution_qty = models.PositiveIntegerField(null=True)
    get_request = models.CharField(max_length=100, null=True)
    timestamp = models.DateTimeField(auto_now=True)


class Teams(models.Model):
    id = models.IntegerField(primary_key=True, null=False)
    team_name = models.CharField(max_length=100, null=True)
    timestamp = models.DateTimeField(auto_now=True)


class TeamMembers(models.Model):
    id = models.IntegerField(null=False, primary_key=True)
    team_member_cec = models.CharField(max_length=30, null=True)
    get_request = models.CharField(max_length=100, null=True)
    timestamp = models.DateTimeField(auto_now=True)


class Discussions(models.Model):
    id = models.IntegerField(null=False, primary_key=True)
    login = models.CharField(max_length=30, null=True)
    title = models.CharField(max_length=30, null=True)
    body = models.CharField(max_length=1000, null=True)
    comments = models.IntegerField(null=True)
    updated_at = models.CharField(max_length=21, null=True)
    get_request = models.CharField(max_length=100, null=True)
    timestamp = models.DateTimeField(auto_now=True)

有什么方法可以覆盖 身份证件 字段并使 PK 这个 timestamp 字段自每次 GET request 运行该字段是否将填充在应用程序生命周期内不会更改的静态数据?

或者,有没有一种方法可以抛弃多表继承体系结构,转而采用不同的方法呢?

我将从中提取的核心指标如下 top contributor to repository , repository with most commits , most replied to comments . 我想能经营一些 filters 在数据上提取这些度量,但我知道这很大程度上依赖于模式设置。

谢谢您!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38339
 
2171 次点击  
文章 [ 1 ]  |  最新文章 6 年前
Iain Shelvington
Reply   •   1 楼
Iain Shelvington    6 年前

将字段设置为主键的方法是

field_name = models.FieldType(primary_key=True)