我的models.py看起來像這樣(的一部分):Django:是否有可能從自定義ModelForm中的content_object模型字段中壓縮值的表單字段?
class GalleryItem(models.Model):
gallery = models.ForeignKey(Gallery)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
def __unicode__(self):
return str(self.object_id)
content_object可以指向任何模型。我想將這種模型的值壓縮到一個表單字段中。 我的表單看起來像這樣:
class GalleryAdminForm(ModelForm):
content_object = TextInput()
def __init__(self, *args, **kwargs):
"""
"""
super(GalleryAdminForm, self).__init__(*args, **kwargs)
class Meta:
model = GalleryItem
是否有可能。我應該在哪裏掛?
是否要在下拉菜單中選擇所有可能的對象,或者所有類型的對象都可以在下拉菜單中選擇? –
只有幾個特定的模型(在我的情況下圖像(),視頻(),聲音())。我不想要所有的content_types。 – Memke