2016-01-20 83 views
0

我刚刚开始使用Django并希望为应用程序创建模型。Django:允许用户将字段添加到模型

我发现Django的功能 - 自动定义为根据模型和 定义字段类型的形式验证和HTML控件类型 - 右模型 非常有用的定义选择的字段设置,我想充分利用它。另外,我想充分利用管理界面。

但是,如果我想允许应用程序的用户向模型添加字段,该怎么办?例如,考虑一个简单的地址簿。我希望用户能够在管理设置中为其所有联系人定义其他属性,即添加传真号码字段,以便可以将传真号码添加到所有联系人。从关系数据库的角度来看,我会有一个带有属性(PK:atr_ID,atr_name,atr_type)的表和属性和联系人之间的N:N关系,以及来自属性和联系人的外键 - 即它会导致3数据库中的表格。对?

但这样我就不能直接在Django模型中定义字段类型。现在最好的做法是什么?我如何使用Djangos功能并允许用户通过管理界面添加aditional/custom字段?

谢谢! :)

最佳 Teconomix

回答

0

我已经使用了这种方法,首先在django-payslip中看到,以允许扩展字段。这提供了一个用于向模型添加字段的结构,从中可以允许用户通过标准查看过程添加/编辑(不需要管理员黑客)。这应该足以让你开始,并且看看django-payslip的源代码(请参阅视图)也提供了查看Mixins和窗体作为如何呈现给用户的示例。

class YourModel(models.Model): 
    extra_fields = models.ManyToManyField(
     'your_app.ExtraField', 
     verbose_name=_('Extra fields'), 
     blank=True, null=True, 
    ) 


class ExtraFieldType(models.Model): 
    """ 
    Model to create custom information holders. 
    :name: Name of the attribute. 
    :description: Description of the attribute. 
    :model: Can be set in order to allow the use of only one model. 
    :fixed_values: Can transform related exta fields into choices. 
    """ 
    name = models.CharField(
     max_length=100, 
     verbose_name=_('Name'), 
    ) 

    description = models.CharField(
     max_length=100, 
     blank=True, null=True, 
     verbose_name=_('Description'), 
    ) 

    model = models.CharField(
     max_length=10, 
     choices=(
      ('YourModel', 'YourModel'), 
      ('AnotherModel', 'AnotherModel'), # which models do you want to add extra fields to? 
     ), 
     verbose_name=_('Model'), 
     blank=True, null=True, 
    ) 

    fixed_values = models.BooleanField(
     default=False, 
     verbose_name=_('Fixed values'), 
    ) 

    class Meta: 
     ordering = ['name', ] 

    def __unicode__(self): 
     return '{0}'.format(self.name) 



class ExtraField(models.Model): 
    """ 
    Model to create custom fields. 
    :field_type: Connection to the field type. 
    :value: Current value of this extra field. 
    """ 
    field_type = models.ForeignKey(
     'your_app.ExtraFieldType', 
     verbose_name=_('Field type'), 
     related_name='extra_fields', 
     help_text=_('Only field types with fixed values can be chosen to add' 
        ' global values.'), 
    ) 

    value = models.CharField(
     max_length=200, 
     verbose_name=_('Value'), 
    ) 

    class Meta: 
     ordering = ['field_type__name', ] 

    def __unicode__(self): 
     return '{0} ({1}) - {2}'.format(
      self.field_type, self.field_type.get_model_display() or 'general', 
      self.value) 
0

可以使用InlineModelAdmin对象。它应该是这样的:

#models.py 
from django.db import models 

class Person(models.Model): 
    name = models.CharField(max_length=100) 

class ContactType(models.Model): 
    name = models.CharField(max_length=100) 

class Contact(models.Model): 
    person = models.ForeignKey(Person, on_delete=models.CASCADE) 
    contact_type = models.ForeignKey(ContactType, on_delete=models.CASCADE) 
    value = models.CharField(max_length=100) 

#admin.py 
from django.contrib import admin 

class ContactInline(admin.TabularInline): 
    model = Contact 

class PersonAdmin(admin.ModelAdmin): 
    inlines = [ 
     ContactInline, 
    ] 

顺便说一句... stackoverflow的问题应该包含一些代码。在提出问题之前,你应该尝试做一些事情。

+0

感谢您的反馈意见以及您对有关缺少代码的评论 - 我会尝试添加一些。 我从原始问题中删除了有关管理界面的部分,以确保问题的重点更加清晰。 Best – teconomix

相关问题