简体   繁体   中英

How to assign `trunsaction_id` to each `user_id`

I want to assign trunsaction_id to each user_id .

The user model is

class User(models.Model):
    trunsaction_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    regist_date = models.DateTimeField(auto_now=True)
    user_id = models.CharField(max_length=200,null=True)
    name = models.CharField(max_length=200,null=True)
    age = models.CharField(max_length=200,null=True)

When I run this code, trunsaction_id is assigned for each registration. Is it possible to assign trunsaction_id to each user_id ? How can I do this?

It's not very clear what you are trying to accomplish.

If you are trying to use a composite primary key, this is not supported by Django. See the documentation here .

If you are trying to get a column with those 2 fields combined, you could try the solution below.

However depending on your need you may be better off doing this in your Querysets. Please clarify what you are trying to achieve.

class User(models.Model):
    trunsaction_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    regist_date = models.DateTimeField(auto_now=True)
    user_id = models.CharField(max_length=200,null=True)
    name = models.CharField(max_length=200,null=True)
    age = models.CharField(max_length=200,null=True)

    @property
    def composite(self):
        return self.user_id + "_" + self.trunsaction_id 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM