2010-11-27 2 views
0

모델에 정의 된 모든 필드를 표시해야하는 양식을 만들고 싶습니다. 모델의 다른 클래스의 핵심입니다. 양식을 생성하기 위해 ModelForm을 사용하고 있습니다.ModelForm을 사용하여 모델에서 양식 만들기, 모델에 많은 외래 키가 있습니다 (하나의 클래스는 다른 클래스의 외래 키입니다).

내 모델은 그래서 누군가가 나를 도와, 또는 나에게 내가 어떤 도움을받을 수있는 곳에서 몇 가지 유용한 링크를 제안 할 수 주시기 바랍니다

class Employee(Person): 
    nickname = models.CharField(_('nickname'), max_length=25, null=True, 
    blank=True) 
    blood_type = models.CharField(_('blood group'), max_length=3, null=True, 
    blank=True, choices=BLOOD_TYPE_CHOICES) 
    marital_status = models.CharField(_('marital status'), max_length=1, 
    null=True, blank=True, choices=MARITAL_STATUS_CHOICES) 
    nationality = CountryField(_('nationality'), default='IN', null=True, 
    blank=True) 
    about = models.TextField(_('about'), blank=True, null=True) 

    dependent = models.ManyToManyField(Dependent, 
    through='DependentRelationship') 

    pan_card_number = models.CharField(_('PAN card number'), max_length=50, 
    blank=True, null=True) 
    policy_number = models.CharField(_('policy number'), max_length=50, 
    null=True, blank=True) 

    # code specific details 
    user = models.OneToOneField(User, blank=True, null=True, 
    verbose_name=_('user')) 
    date_added = models.DateTimeField(_('date added'), auto_now_add=True) 
    date_modified = models.DateTimeField(_('last modified'), auto_now=True) 

    @models.permalink 
    def get_absolute_url(self): 
    return ('contacts_employee_detail', [str(self.id)]) 

class Person(models.Model): 
    """Person model""" 

    title = models.CharField(_('title'), max_length=20, null=True, blank=True) 
    first_name = models.CharField(_('first name'), max_length=100) 
    middle_name = models.CharField(_('middle name'), max_length=100, null=True, 
     blank=True) 
    last_name = models.CharField(_('last name'), max_length=100, null=True, 
     blank=True) 
    suffix = models.CharField(_('suffix'), max_length=20, null=True, 
     blank=True) 

    slug = models.SlugField(_('slug'), max_length=50, unique=True) 

    phone_number = generic.GenericRelation('PhoneNumber') 
    email_address = generic.GenericRelation('EmailAddress') 
    address = generic.GenericRelation('Address') 

    date_of_birth = models.DateField(_('date of birth'), null=True, blank=True) 
    gender = models.CharField(_('gender'), max_length=1, null=True, 
    blank=True, choices=GENDER_CHOICES) 

class Address(models.Model): 
"""Street Address model""" 

    TYPE_CHOICES = (
    ('c', _('correspondence address')), 
    ('p', _('present address')), 
    ('m', _('permanent address')), 
) 

    address_type = models.CharField(_('address type'), max_length=1, 
    choices=TYPE_CHOICES) 

    content_type = models.ForeignKey(ContentType, 
    limit_choices_to={'app_label': 'contacts'}) 
    object_id = models.PositiveIntegerField() 
    content_object = generic.GenericForeignKey() 

    street = models.TextField(_('street'), blank=True, null=True) 
    city = models.CharField(_('city'), max_length=200, blank=True, null=True) 
    province = models.CharField(_('State/UT'), max_length=200, blank=True, 
    null=True) 
    post_code = models.CharField(_('postal code'), max_length=15, blank=True, 
    null=True) 
    country = CountryField(_('country'), default='IN') 

    date_added = models.DateTimeField(_('date added'), auto_now_add=True) 
    date_modified = models.DateTimeField(_('date modified'), auto_now=True) 

처럼 보인다. 고맙습니다!!!

+1

정확히 무엇이 문제입니까? – Kugel

+0

답변 됨 .... – FallenAngel

답변

0

Here is the documentation ...

기본 사용법은 다음과 같습니다

class EmployeeForm(ModelForm): 
    class Meta: 
     model = Employee 
     fields = ('somefield','otherfield') 

등 ...

필드는 마녀 필드를 정의하는 데 사용되는 선택적 인수가 양식에 표시 될 것입니다 ..

class EmployeeForm(ModelForm): 
    otherfield = forms.CharField(...) 
    class Meta: 
     model = Employee 
     fields = ('somefield','otherfield') 

해당 필드를 재정의 할 수도 있습니다. 의미는 Employee 모델에서 양식을 만들고 "somefield"및 "otherfield"를 양식 필드로 추가하고 somefield는 모델에서 직접 채 웁니다. 그러나 otherfield는 양식 클래스에서 재정의 한 것처럼 정의됩니다.

EDIT : 작은 변경에는 재정의가 사용되므로 필드의 데이터 유형을 변경하는 것이 옳지 않습니다. 필드에 동일한 이름을 지정하는 한 아무런 문제가 없습니다. 관련 모델 필드 그래서 ... 양식 필드의 이름을 사용하지 : 필드 이름이 동일하기 때문에

class SomeModel(Model): 
    somefield = CharField() 

class SomeForm(ModelForm): 
    somefield = Charfield(Widget=...) 
    class Meta: 
     model = SomeModel 

가, 거기 아무 문제 ... 오버라이드 (override)에 대한 기본 이유는, 당신은에 위젯을 사용하실 수 있습니다 외모를 바꾸다. text 필드를 단일 행 Charfield처럼 보이게 만들거나 텍스트 필드의 col과 행을 정의하거나 datetime 필드에 simlpe datepicker를 추가하는 것과 같은 일부 속성을 전달할 수 있습니다. 또는 선택 매개 변수를 사용하여 값 레이블 쌍으로 필드를 채우려는 경우 ...

어떤 종류의 데이터 기반 변경도 수행해야합니다. 데이터베이스 수준의 오류가 발생할 수 있습니다.

+0

업데이트 됨 그러나이 작업을 수행하는 올바른 방법은 필드를 재정의하는 bcoz가 가능하지만 해당 필드의 값이 데이터베이스의 어딘가에 저장되고 필드를 무시하면됩니다. 데이터베이스의 실제 위치로 이동하십시오. – Prateek

관련 문제