簡體   English   中英

在django模型中如何用`timestamp`作為主鍵覆蓋`id`作為主鍵?

[英]How to overwrite `id` as primary key with `timestamp` as primary key in django models?

我正在構建一個Django ETL引擎,它使用企業API從GitHub中提取數據,以收集內部公司協作的指標。 我設計了一些模式,我現在意識到由於ORM自動設置的PK(主鍵)而無法擴展。 提取的一個主要特征是獲取創建存儲庫的人的id ,在帖子上發表評論等。

我最初的想法是讓ORM自動將id設置為PK但這不起作用,因為GET請求將每周運行一次並且它將引發錯誤,導致覆蓋ID主鍵失敗。

我做了一些研究,一個可能的解決方案是創建一個這里引用的元類: Django模型主鍵作為一對

但我不確定創建一些元類是否會打破元類的全部要點。

這是我為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)

有沒有辦法覆蓋id字段並使PK成為timestamp字段,因為每次運行GET request該字段將填充靜態數據,該靜態數據在應用程序的生命周期內不會改變?

或者,有沒有辦法拋棄多表繼承架構並尋找不同的東西?

我將從中提取的核心指標是諸如top contributor to repository repository with most commits top contributor to repositoryrepository with most commits top contributor to repository repository with most commitsmost replied to comments 我希望能夠對數據運行某種filters ,以便提取這些指標,但我知道這很大程度上依賴於架構設置。

謝謝!

將字段設置為主鍵的方法是

field_name = models.FieldType(primary_key=True)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM