简体   繁体   English

在带有 Django Rest Framework 的 Django 模型中使用 uuid 作为外键

[英]Use uuid as foreign Key in a model in Django with Django Rest Framework

I don't know if the title of the post is right.不知道帖子标题对不对。 I'll explain what I want to do and you could tell me what's the best way.我会解释我想要做什么,你可以告诉我什么是最好的方法。

I want to have an "id" (Int) as Primary Key and a "uuid" (uuid) as field in my User model.我想在我的用户模型中有一个“id”(Int)作为主键和一个“uuid”(uuid)作为字段。 So, I want to link the tables with the "id", because it's faster, but I want that the front end just see the "uuid" and never the "id", because is a bit safer.所以,我想用“id”链接表,因为它更快,但我希望前端只看到“uuid”而不是“id”,因为这样更安全。

My problem is that I've a "message" model.我的问题是我有一个“消息”模型。 It looks so:看起来是这样的:

class Message(models.Model):
    created = models.DateTimeField(auto_now_add=True)

    type = models.CharField(_('type'), choices=MESSAGE_TYPE, default='Invitation', max_length=100)
    content = models.TextField(_('content'), blank=False)
    sender = models.ForeignKey(User, related_name='sender_message', verbose_name=_("Sender"), )
    recipient = models.ForeignKey(User, related_name='receiver_message', null=True, blank=True,
                                  verbose_name=_("Recipient"))
    url_profile_image = models.URLField(_('url_profile_image'), max_length=500, blank=True, default='')

    class Meta:
        ordering = ('created',)

And as you can see, "sender" and "recipient" are linked to my User with a ForeignKey .正如您所看到的,“发件人”和“收件人”通过ForeignKey链接到我的用户。 But that ForeignKey returns as the user Id.但是该ForeignKey作为用户 ID 返回。

  {
    "url": "http://127.0.0.1:8000/users/messages/4/",
    "id": 4,
    "type": "invitation_accepted",
    "content": "Sure",
    "sender": 4,
    "recipient": 1,
    "url_profile_image": ""
  }

4 is the id of the "sender" and 1 is the id of the "recipient". 4 是“发件人”的 id,1 是“收件人”的 id。 But I would like that the front end just see the "uuid" of the sender and the "uuid" of the recipient.但我希望前端只看到发件人的“uuid”和收件人的“uuid”。

My serializer looks so:我的序列化程序看起来是这样的:

class MessageSerializer(serializers.ModelSerializer):

    class Meta:
        model = Message
        fields = ('url', 'id', 'type', 'content', 'sender', 'recipient', 'url_profile_image')

So, I thought that maybe there're two ways to do what I want.所以,我想也许有两种方法可以做我想做的事。

Either use the uuid as Foreign Key or do something in the serializer and get the uuid of the user and return it in "sender" and "recipient".要么使用 uuid 作为外键,要么在序列化程序中做一些事情并获取用户的 uuid 并在“发送者”和“接收者”中返回它。

My view is quite simple:我的观点很简单:

class MessageViewSet(viewsets.ModelViewSet):
    queryset = Message.objects.all()
    serializer_class = MessageSerializer

(don't worry about the queryset, it looks crazy, but it's so :) that kind of message is not private :D ) (不要担心查询集,它看起来很疯狂,但确实如此:) 这种消息不是私密的:D)

Maybe someone can help me.也许有人可以帮助我。

You can try你可以试试

class MessageSerializer(serializers.ModelSerializer): 
    sender = serializers.ReadOnlyField(source='sender.uid')
    recipient = serializers.ReadOnlyField(source='recipient.uid')

    class Meta: 
        model = Message 
        fields = ('url', 'id', 'type', 'content', 'sender', 'recipient', 'url_profile_image')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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