与Django和rest框架的关系添加替换字段 [英] Relationships with Django and rest framework add replace fields

查看:105
本文介绍了与Django和rest框架的关系添加替换字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用户对象与休息框架有一个 avatar_id 和一个 cover_id 。但是,我不想将其显示给API,而是将它作为实际的头像URL和封面URL。

My user object with rest framework has an avatar_id and a cover_id. But Instead of displaying that to the API, I want it to be the actual avatar URL and cover URL already.

我的用户 model:

avatar_id = models.IntegerField()
cover_id = models.IntegerField()

我的 UserAvatar 模型:

id = models.IntegerField(primary_key=True)
user_id = models.IntegerField()
file_id = models.IntegerField()

我的文件 model:

id = models.IntegerField(primary_key=True)
filename = models.CharField(max_length=255)

UserCover 相同的概念。

如何从 / users / 的结果中删除 avatar_id ,并添加一个头像字段实际的头像文件名?

How do I remove the avatar_id from the results of /users/ and add a avatar field with the actual avatar filename?

推荐答案

我不知道我是否正确理解你的问题,墨水的问题是。阅读你的问题,我假设你是一个初学者,所以我这样回答。对不起,如果不是这样的话。

I'm not sure I understand your question correctly, but here what I think the problems are. Reading your question, I assumed that you are a beginner, so I answered as such. Sorry if it's not the case.


  • 你不需要添加id字段,它由Django自动完成,因为所有表都需要首要的关键。您只需要将其命名为id以外的其他名称即可定义PK。

  • 您应该真正阅读 Django教程,介绍了如何定义模型。 User.cover_id和UserAvatar.file_id应定义为ForeignKey。如果你不知道一个外键是什么,那就停止玩Django,然后阅读数据库教程。

  • 已经有一个模型和一组到管理您的用户。你应该使用它们。例如,用户个人资料是扩展用户模型

  • 如果强制用户在一组预定义的化身中选择一个头像,那么您要做的就可以了。如果用户可以选择任何头像(上传),那么您应该使用 OneToOneField 或直接放在用户模型(或配置文件)中。

  • You don't need to add the id fields, it's done automatically by Django because all tables need a primary key. You define a PK only when you need to name it something else than 'id'.
  • You should really read the Django tutorial which explains how to define models. User.cover_id and UserAvatar.file_id should be defined as ForeignKey. If you don't know what a foreign key is, then stop playing with Django and read a database tutorial before.
  • There's already a model and a set of classes to manage your users in Django. You should use them. For example, a "user profile" is the right way to extend the user model.
  • If you force the users to choose one avatar in a set of predefined avatars, then what you want to do is ok. If the users can choose any avatar (upload), then you should use OneToOneField or put it directly in the user model (or profile).

我不知道是什么是code> UserCover ,但这是您的模型可能如下所示:

I don't know what is a UserCover, but here's what your models could look like:

from django.contrib.auth.models import User

class UserProfile(models.Model):
    # Link to Django normal User (name, email, pass)
    user = models.ForeignKey(User, unique=True)

    # Any information that a user needs, like cover, wathever that is, age, sexe, etc.
    avatar = models.CharField(max_length=255)

或者像这样,如果一个会被重复使用:

Or like this if a will be reused often :

class Avatar(models.Model):
    # name = ...
    # description = ...
    path = models.CharField(max_length=255)

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    avatar = models.ForeignKey(Avatar, unique=True)

    # other data

这篇关于与Django和rest框架的关系添加替换字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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