Django数据库设计(Django database design)

编程入门 行业动态 更新时间:2024-10-20 20:45:01
Django数据库设计(Django database design)

我正在写一个在线工资单系统,我在设计数据库时遇到了问题。 客户可以签约我们的计划。 他的信息存储在“客户”模型中。 客户可以拥有一个或多个公司(模型“公司”)。 每个公司都有员工(模型“员工”),员工有一套其他模型用于地址,税务,社会保障等。如果员工的数据发生变化(例如婚礼后的新名称,或者更改了税务数据)将注册具有更改日期的新记录(模型中的字段“xxx_valid”)。 由于历史原因,旧记录必须保存在数据库中。

这是我的模特:

class Client (models.Model): client_name = models.CharField (...) .... class Company (models.Model): clientID = models.ForeignKey(Client) company_name = models.CharField (...) .... class Staff (models.Model): clientID = models.ForeignKey(Client) companyID = models.ForeignKey(Company) staff_nr = models.IntegerField (....) staff_valid = models.DateField (....) staff_name = models.CharField (...) .... class StaffTax (model.Model): clientID = models.ForeignKey(Client) companyID = models.ForeignKey(Company) staffID = models.ForeignKey(Staff) tax_valid = models.DateField (....) tax_nr = models.CharField (...) .... class StaffSocSec (model.Model): clientID = models.ForeignKey(Client) companyID = models.ForeignKey(Company) staffID = models.ForeignKey(Staff) sosec_valid = models.DateField (....) sosec_nr = models.CharField (....) ....

如果员工没有历史,一切都很好。 现在假设,工作人员在模型人员(现场staff_valie)中获得了一个新的时期,因为他的地址已经改变。 这个新记录将获得一个新的ID(pk)如果工资单计算开始,将使用最新的工作人员记录。 使用StaffID进行StaffTayx等的查询集。 但是税务信息与模型Staff中的第一条(旧)记录相关,并且查询集找不到任何记录。

当然我可以忽略ID,在模型StaffTax(和其他人)中将staffID定义为普通的Charfield,并使用这个正常的Charfield而不是使用primery key id来创建我的queryset。 但是我会在模型中自动创建类似自动创建选项的功能。

我也不能设置staffID具有primery密钥,因为其他客户端可能具有相同的StaffID并且我得到一个唯一的错误。

有人知道如何解决这个问题吗?

非常感谢!

I am writing an online payroll system and I am having problems to design my database. A client can contract our program. His information is stored in model "Client". A client can have one or more companies (model "Companies"). Each company has staff (model "Staff") and a staff has a set of other models for adress, tax, social security etc. If a staff has changes in his data (for example a new name after a wedding, or changes in the tax data) a new record with the date of the change will be registered (field "xxx_valid" in models). The old record has to be kept in database for historical reasons.

Here are my models:

class Client (models.Model): client_name = models.CharField (...) .... class Company (models.Model): clientID = models.ForeignKey(Client) company_name = models.CharField (...) .... class Staff (models.Model): clientID = models.ForeignKey(Client) companyID = models.ForeignKey(Company) staff_nr = models.IntegerField (....) staff_valid = models.DateField (....) staff_name = models.CharField (...) .... class StaffTax (model.Model): clientID = models.ForeignKey(Client) companyID = models.ForeignKey(Company) staffID = models.ForeignKey(Staff) tax_valid = models.DateField (....) tax_nr = models.CharField (...) .... class StaffSocSec (model.Model): clientID = models.ForeignKey(Client) companyID = models.ForeignKey(Company) staffID = models.ForeignKey(Staff) sosec_valid = models.DateField (....) sosec_nr = models.CharField (....) ....

Everything works fine if a staff has no history. Now lets say, a staff gets a new period in model Staff (field staff_valie) because his address has changed. This new record will get a new id (pk) If the payroll calculacion is started, the latest record of Staff will be used. Querysets to StaffTayx etc are made using the StaffID. But the tax information are related with the first (old) record in model Staff and the queryset woun't find any records.

Of course I could ignore ID, define staffID in the models StaffTax (and others) as a normal Charfield and make my queryset using this normal Charfield instead of using the primery key id. But I would loose a lot of funcionality like automatic created options for in a modelformset.

Nor I can set staffID has primery key, because an other client could have the same StaffID and I get an unique error.

Anybody has a idea how to solve this problem?

Thanks a lot guys!

最满意答案

听起来您只是希望能够保留模型数据的更改历史记录。 有几个选项,请参阅Django包中的这个列表 。

在编辑历史和查看报告方面, django-simple-history可能是一个很好的匹配。 这有管理员集成和轻松查询历史记录的能力。

另外

It sounds like you just want to be able to keep a history of changes to your model's data. There are a few options out there, see this list on Django packages.

In terms of editing the history and viewing reports, django-simple-history could be a good match. That has admin integration and the ability to query the history easily.

Alternatively

更多推荐

本文发布于:2023-07-31 01:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340520.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   Django   design   database

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!