在django模型中实现每个领域的隐私的最佳方式 [英] best way to implement privacy on each field in model django

查看:158
本文介绍了在django模型中实现每个领域的隐私的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为用户在模型的现场级别提供隐私设置。所以用户可以决定他想要显示哪些数据以及他想隐藏哪些数据。

I am trying to provide privacy settings at the model's field level for users. So a user can decide which data he wants to display and which data he wants to hide.

示例:

class Foo(models.Model):
    user = models.OneToOneField("auth.User")
    telephone_number = models.CharField(blank=True, null=True, max_length=10)
    image = models.ImageField(upload_to=get_photo_storage_path, null=True, blank=True)

我想为用户提供一个选项,以选择他想要显示的字段,以及他不显示的字段。
考虑用户不想显示电话号码,所以他应该有选项。

I want to provide an option to the user to select the fields which he wants to display and which he doesn't. Consider a user doesn't want to display telephone_number, so he should have the option for that.

哪个是最好的方法?

推荐答案

您可以在模型中创建一个 CommaSeparatedIntegerField 字段,并使用它来存储用户想要隐藏的field_names(表示field_name的整数)。

You can create a CommaSeparatedIntegerField field inside the model, and use it to store a list of field_names (Integers that denote a field_name) that the user wants to hide.

您可以在models.py中的一个常量之间创建field_names和整数之间的映射。并检查用户检查的字段名称。

You can create a mapping between the field_names and integers as a constant inside your models.py. And check whichever field_names are those the the user had checked.

示例映射:

FIELD_NAME_CHOICES = (
    (1, Foo._meta.get_field('telephone_number')),
    (2, Foo._meta.get_field('name')),
    .
    .
)

检查以下链接以获取参考 https://docs.djangoproject.com/en/1.8/ref/models/fields/# commaseparatedintegerfield

Check the following link for reference https://docs.djangoproject.com/en/1.8/ref/models/fields/#commaseparatedintegerfield

这篇关于在django模型中实现每个领域的隐私的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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