2013-06-21 1 views
3

사용자 content_type 선택을 기반으로 동적 양식을 어떻게 만들거나 처리 할 수 ​​있습니까?Django - GenericForeignKey의 동적 양식

내가 모델이 다른 모델에 대한 일반적인 키 보유하는 객체 추가하는 뷰/템플릿을 쓰고 있어요 :

class MainModel(models.Model): 
     title = models.CharField() 
     author = models.ForeignKey(User) 
... 
     content_type = models.ForeignKey(ContentType) 
     object_id = models.PositiveIntegerField() 
     content_object = generic.GenericForeignKey('content_type', 'object_id') 

class FileModel(models.Model): 
    file = models.FileField() 
    content = generic.GenericRelation(MainModel) 


class UrlModel(models.Model): 
    url = models.UrlField() 
    content = generic.GenericRelation(MainModel) 

내가 (객체를 추가하는) 사용자에게 하나의 페이지를주고 싶습니다 어디 그/그녀는 제목, 저자, ... 필드를 채우고 content_type (드롭 다운 목록)을 선택하고 선택한 content_type의 필드를 채울 수 있습니다. FileModel이 선택된 경우 파일. 그런 다음 하나의 "보내기"버튼이있는보기로 보내야합니다. 여기서는 선택한 content_type 및 MainModels의 적절한 객체가 만들어집니다. 일부 ajax/jquery를 사용하여 선택한 content_type의 필드를 사용하여 초기 양식을 "확장"하는 방법을 생각했지만 올바른 방법에 대한 다른 제안을 환영합니다.

답변

1

누군가가이 혜택을 볼 수 있습니다.

다른 접두사가있는 2 개의 양식이 생겼습니다. 1) MainModel의 경우 2) 선택한 content_type에 대해 동적으로 작성한 경우 2) 양식 2)의 필드는 ajax/jquery로드 함수를 통해 (content_type 선택의) 사이트에 동적으로 추가됩니다. 데이터 &의 적절한 처리가 뷰에서 처리됩니다.