【问题标题】:Make a form from models using ModelForm, Models have many Foreignkeys( one class is the foreign key for the other.)使用 ModelForm 从模型制作表单,模型有很多外键(一个类是另一个类的外键。)
【发布时间】:2010-11-27 11:00:50
【问题描述】:

我想制作一个表单,它应该显示模型中定义的所有字段,这些字段是否包含模型中某个其他类的外键。我正在使用 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)

所以请如果有人可以帮助我,或者建议我一些有用的链接,我可以从中获得一些帮助。谢谢!!!

【问题讨论】:

    标签: python django forms


    【解决方案1】:

    Here is the documentation...

    基本用法是:

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

    等等……

    fields 是一个可选参数,用于定义将在表单上呈现的女巫字段...您也可以使用以下内容覆盖某些字段

    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 看起来像单行字符字段)或传递一些属性(例如定义文本字段的列和行,或添加simlpe datepicker 到日期时间字段。或者您可能希望使用选择参数来填充具有 value-label 对的字段...

    您必须避免任何类型的基于数据的更改,否则可能会出现数据库级错误。

    【讨论】:

    • 但它是否是正确的方法来做这件事,bcoz 覆盖该字段是可能的,但该字段的值将存储在数据库中的某个位置,如果我覆盖这些字段,它会进入数据库中的实际位置。
    猜你喜欢
    • 1970-01-01
    • 2017-08-30
    • 2011-03-05
    • 2023-02-04
    • 1970-01-01
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多