如何在Django中将2个属性设置为主键? [英] How to set 2 attributes to primary key together in Django?

查看:87
本文介绍了如何在Django中将2个属性设置为主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django中有一个模型:

I have a model in Django:

 class Subject(models.Model):
    level = models.CharField(max_length=50)
    subject_name = models.CharField(max_length=50)
    teacher_name = models.ForeignKey(Teacher, on_delete=models.CASCADE)
    total_seats = models.IntegerField()
    subject_details = models.CharField(max_length=50)

对于 Subject 表,我希望 level subject_name 一起作为主键.实际上,我不希望任何其他对象具有相同的名称和级别.我知道我可以使用 unique_together ,但是我在哪里提到 primary_key = True ?

For the Subject table I want the level and the subject_name together to be primary keys. In fact, I dont want any other objects to have the same name and level. I know I can use unique_together but where do I mention the primary_key = True?

推荐答案

不要.Django无法使用复合主键.这在文档:

You don't. Django does not work with composite primary keys. This is specified in the documentation:

每个模型都需要恰好一个字段来具有 primary_key = True (明确声明或自动添加).

Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added).

In the FAQ section it also continues with:

Django模型是否支持多列主键?

Do Django models support multiple-column primary keys?

.仅支持单列主键.

但这实际上不是问题,因为没有停止的机会您无需添加其他约束(使用 unique_together 模型选项或直接在数据库中创建约束),以及在该级别上加强唯一性.单列主键是管理界面等功能所需的功能;例如,您需要一个值来指定要编辑或删除的对象.

But this isn’t an issue in practice, because there’s nothing stopping you from adding other constraints (using the unique_together model option or creating the constraint directly in your database), and enforcing the uniqueness at that level. Single-column primary keys are needed for things such as the admin interface to work; e.g., you need a single value to specify an object to edit or delete.

该功能经常被要求使用(例如,参见 Django票证),但尚未实施.这可能会很麻烦,首先,很多现有的Django工具都需要更新(例如 JOIN 应该用两个键 FOREIGN KEY 完成)则应导致构造了两个或多个字段,依此类推.)但是,另一个可能甚至更严重的问题可能是在Django之上构建的大量程序包,这些程序包假设主键不是复合键.因此,它将破坏Django生态系统"中的许多软件包.

It is a feature that is often requested (see for example this Django ticket), but it was not implemented. It will probably be quite cumbersome, first of all a lot of existing Django tooling will need to be updated (for example JOINs should be done with the two keys, FOREIGN KEYs should then result in two or more fields constructed, etc.). But another, and probably even more severe problem might be the large number of packages built on top of Django that make the assumption that the primary key is not a composite. It would thus break a lot of packages in the Django "ecosystem".

有些软件包如 django-compositekey > [GitHub] 旨在实现这一目标.但是最后一次更新是在2014年10月进行的.

There are some packages like django-compositekey [GitHub] that aim to implement this. But the last update is made in october 2014.

不使其成为主键本身不是问题.实际上,Django的 GenericForeignKey [Django-doc] 仅在主键都是相同类型时才有效.因此,使用 unique_together 就足够了.通常,这也会在数据库的一侧产生 UNIQUE INDEX .

It is not per se a problem not to make it a primary key. In fact Django's GenericForeignKey [Django-doc] only works if the primary keys are all of the same type. So using unique_together should be sufficient. Normally this will also make a UNIQUE INDEX at the databaes side.

这篇关于如何在Django中将2个属性设置为主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆