How to create user defined fields in Django

十年热恋 提交于 2020-01-02 10:11:09

问题


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


回答1:


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)


来源:https://stackoverflow.com/questions/1463242/how-to-create-user-defined-fields-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!