2

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

2
  • You may wish to clarify your goals. Do you want to simply associate arbitrary metadata with these users, or do you need to lookup users by specific fields? Commented Sep 23, 2009 at 0:20
  • You might me looking for this reference: stackoverflow.com/a/7934577/497056 Commented Feb 19, 2012 at 13:31

1 Answer 1

4

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.