簡體   English   中英

使用ModelForm從模型創建表單,模型有許多外鍵(一個類是另一個的外鍵。)

[英]Make a form from models using ModelForm, Models have many Foreignkeys( one class is the foreign key for the other.)

我想制作一個表格,該表格應顯示模型中定義的所有字段,並且這些字段包括模型中某些其他類的外鍵。 我正在使用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)

因此,如果有人可以幫助我,或者向我建議一些有用的鏈接,我可以從中獲得幫助。 謝謝!!!

這是文檔 ...

基本用法是:

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的定義就像您在表單類中覆蓋它一樣。 ..

編輯:覆蓋用於較小的更改,因此,更改字段的數據類型是不正確的...只要為字段指定相同的名稱,就沒有問題,它將使用來匹配相關的模型字段表單字段的名稱...因此:

class SomeModel(Model):
    somefield = CharField()

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

由於字段名稱是等效的,所以沒有問題...覆蓋的基本原因是,您可能希望使用小部件來更改表單字段的外觀(使TextField看起來像單行Charfield)或傳遞一些屬性(例如定義文本字段的行和行,或在日期時間字段中添加簡單的日期選擇器;或者您可能希望使用choices參數用值-標簽對填充字段...

您必須避免任何基於數據的更改,否則,可能會發生數據庫級錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM