开发者

Model design question: custom fields at runtime?

I have some newbie questions about Django.

I want to write a generic ticket-management system开发者_运维百科, where the administrator of the site should be able to add custom fields to a ticket. It seems that the database tables are generated on initialization, so it is not clear to me how to add custom fields at runtime. One way is to have a long list of fields of different types, all nullable, and let the administrator rename/select the fields she needs. Is there a better design?

Thanks!


I'm currently in charge of maintaining a similar site where a treatment for a medical condition is listed and there can be arbitrary number of "cases" which are user-posted experiences for that treatment/condition combo attached.

The method my company used to set it up was to have an Entry object which would be analogous to the custom field you described, which has a Foreign Key referencing the treatment/condition to which it belongs.

Then when we want to get all the entries for a particular treatment/condition combo, we simply do an

Entry.objects.filter(condition=ID)

So, in your case, I would suggest having a Ticket model, and an "Entry" style model which contains a Foreign Key reference to the Ticket to which it belongs.


I would make something like the code below. Store extra attributes in an attribute model. Store the values in AttributeValue.

class Ticket(models.Model):
    name = models.CharField(max_length=200)


class Attribute(models.Model):
    name = models.CharField(max_length=200)


class AttributeValues(models.Model):
    attribute = models.ForeignKey(Attribute)
    ticket = models.ForeignKey(Ticket)
    value = models.CharField(max_length=200)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜