我有一个电话模型,许多不同的模型经常将其用作通用关系。我不知道如何将它包含在这些模型的创建/更新表单中……在 forms.ModelForm 子类中包含额外字段的想法是好是坏……有点像这样:
###### models.py
class UpstreamContactModel(models.Model):
client = models.ForeignKey(UpstreamClientModel,
related_name='contacts')
contact_type = models.CharField(max_length=50, default='Main',
blank=True, null=True)
name = models.CharField(max_length=100, unique=True)
job_title = models.CharField(max_length=50, blank=True, null=True)
email = models.EmailField(blank=True, null=True)
skype_id = models.CharField(max_length=30, blank=True, null=True)
phones = generic.GenericRelation(Phone)
notes = models.TextField(blank=True, null=True)
def __unicode__(self):
return self.name
class Meta:
verbose_name = 'Contact'
class Phone(models.Model):
info = models.CharField('Eg. Office, Personal, etc',
max_length=15, blank=True)
number = models.CharField('Phone numbes', max_length=20)
# generic relationships so I can attach to other objects later on
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
def __unicode__(self):
return self.number
##### forms.py
class ContactForm(forms.ModelForm, BaseValidationForm):
info = forms.CharField(max_length=15)
number = forms.CharField(max_length=20)
class Meta:
model = UpstreamContactModel
def clean(self):
???
def save(self):
???
我一直试图找出人们在涉及通用关系时如何处理 CRUD,但到目前为止我还没有成功。