make primary key with 2 fields in Django?

后端 未结 3 887
别那么骄傲
别那么骄傲 2020-12-18 09:53

I want to make a primary key from 2 fields in Django.

Fields are below

- current Time 
- userId

How Can I do

3条回答
  •  一向
    一向 (楼主)
    2020-12-18 10:36

    If the key has to be single-column:

    id is added "out-of-the-box" (variable.id) and for current time just add

    from django.db import models
    
    date = models.DateTimeField( auto_now_add=True)
    

    in yourapp/models.py

    so you have your 2 values, out of witch you can make your key in any shape you want.

    if you want multi-column-key then read the other answer. Hope to help you.

提交回复
热议问题