如何在Django中创建用户定义的字段 [英] How to create user defined fields in Django

查看:207
本文介绍了如何在Django中创建用户定义的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在使用几种不同的模型,即帐户,联系人等进行Django应用程序,每个模型都有不同的字段集。除了现有的字段之外,我还需要允许我的每个用户定义自己的字段。我已经看到几种不同的方法来实现这一点,从拥有大量的CustomFields,并将自定义名称映射到每个用户使用的每个字段。我也似乎建议实现用户定义字段的复杂映射或XML / JSON样式存储/检索。



所以我的问题是,有没有人在Django应用程序中实现了用户定义的字段?如果是这样,你是如何做到的,以及你在整体实施方面的经验(稳定性,性能等)?



更新:我的目标是让我的每一个用户创建n个每个记录类型(帐户,联系人等),并将用户定义的数据与每个记录相关联。所以例如,我的一个用户可能想要将SSN与他的每个联系人相关联,所以我需要为他创建的每个联系人记录存储该附加字段。



谢谢!



标记

解决方案

如果您要使用一个ForeignKey?



此代码(未经测试和演示)假定有一个系统范围的自定义字段。要使用户特定,您可以在CustomField类中添加一个user = models.ForiegnKey(User)。

  class Account(models.Model):
name = models.CharField(max_length = 75)

#...

def get_custom_fields(self):
return CustomField.objects.filter(content_type = ContentType.objects.get_for_model(Account))
custom_fields = property(get_fields)

class CustomField(models.Model):

一个字段摘要 - 它描述了该字段。用户配置的每个自定义字段都有一个


name = models.CharField(max_length = 75)
content_type = models.ForeignKey(ContentType)

class CustomFieldValueManager(models.Manager):

get_value_for_model_instance(self,model) :
content_type = ContentType.objects.get_for_model(model)
return self.filter(model__content_type = content_type,model__object_id = mod el.pk)


class CustomFieldValue(models.Model):

字段实例 - 包含实际数据。有许多这些,对于
每个值对应于给定模型的CustomField。

field = models.ForeignKey(CustomField,related_name ='instance')
value = models.CharField(max_length = 255)
model = models.GenericForeignKey()

objects = CustomFieldValueManager()

#如果要枚举自定义字段及其值,它将看起来像
#如下所示:

account = Account.objects.get(pk = 1)
在account.custom_fields中的字段:
print field.name,field.instance.objects.get_value_for_model_instance(account)


Ok, I am working on a Django application with several different models, namely Accounts, Contacts, etc, each with a different set of fields. I need to be able to allow each of my users to define their own fields in addition to the existing fields. I have seen several different ways to implement this, from having a large number of CustomFields and just mapping a custom name to each field used by each user. I have also seem recommendations for implementing complex mapping or XML/JSON style storage/retrieval of user defined fields.

So my question is this, has anyone implemented user defined fields in a Django application? If so, how did you do it and what was your experience with the overall implementation (stability, performance, etc)?

Update: My goal is to allow each of my users to create n number of each record type (accounts, contacts, etc) and associate user defined data with each record. So for example, one of my users might want to associate an SSN with each of his contacts, so I would need to store that additional field for each Contact record he creates.

Thanks!

Mark

解决方案

What if you were to use a ForeignKey?

This code (untested and for demo) is assuming there is a system-wide set of custom fields. To make it user-specific, you'd add a "user = models.ForiegnKey(User)" onto the class CustomField.

class Account(models.Model):
    name = models.CharField(max_length=75)

    # ...

    def get_custom_fields(self):
        return CustomField.objects.filter(content_type=ContentType.objects.get_for_model(Account))
    custom_fields = property(get_fields)

class CustomField(models.Model):
    """
    A field abstract -- it describe what the field is.  There are one of these
    for each custom field the user configures.
    """
    name = models.CharField(max_length=75)
    content_type = models.ForeignKey(ContentType)

class CustomFieldValueManager(models.Manager):

    get_value_for_model_instance(self, model):
        content_type = ContentType.objects.get_for_model(model)
        return self.filter(model__content_type=content_type, model__object_id=model.pk)


class CustomFieldValue(models.Model):
    """
    A field instance -- contains the actual data.  There are many of these, for
    each value that corresponds to a CustomField for a given model.
    """
    field = models.ForeignKey(CustomField, related_name='instance')
    value = models.CharField(max_length=255)
    model = models.GenericForeignKey()

    objects = CustomFieldValueManager()

# If you wanted to enumerate the custom fields and their values, it would look
# look like so:

account = Account.objects.get(pk=1)
for field in account.custom_fields:
    print field.name, field.instance.objects.get_value_for_model_instance(account)

这篇关于如何在Django中创建用户定义的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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