I'm creating a simple contacts app in django and I want to allow everyone to have more than a single phone number.
我正在django中创建一个简单的联系人应用程序,我想让每个人都拥有多个电话号码。
My models.py looks like this:
我的models.py看起来像这样:
from django.db import models
class Person(models.Model):
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
birthday = models.DateField(null=True, blank=True)
def __str__(self):
return ' '.join([self.first_name, self.last_name])
class Phone(models.Model):
phone_number = models.CharField(max_length=12)
description = models.CharField(max_length=25)
person = models.ForeignKey(Person, on_delete=models.CASCADE)
def __str__(self):
return self.phone_number
I need to create a form where I can add the core contact info and as many phone numbers as wanted.
我需要创建一个表单,我可以添加核心联系信息和所需的电话号码。
I have a ModelForm like
我有一个ModelForm
class PersonForm(forms.ModelForm):
class Meta:
model = Person
fields = '__all__'
How do I create a phone_number field which allows to insert the phone number?
如何创建允许插入电话号码的phone_number字段?
1 个解决方案
#1
1
Here is a nice tutorial that is perhaps a bit more helpful than the documentation if you're struggling: http://whoisnicoleharris.com/2015/01/06/implementing-django-formsets.html
这是一个很好的教程,如果你正在努力,可能比文档更有帮助:http://whoisnicoleharris.com/2015/01/06/implementing-django-formsets.html
It goes through using formsets which allow you to have and use more than one form of the same type on a page. And also shows you a JQuery plugin that allows you to add and remove forms dynamically.
它通过使用formsets,允许您在页面上拥有和使用多个相同类型的表单。并且还向您展示了一个JQuery插件,允许您动态添加和删除表单。
#1
1
Here is a nice tutorial that is perhaps a bit more helpful than the documentation if you're struggling: http://whoisnicoleharris.com/2015/01/06/implementing-django-formsets.html
这是一个很好的教程,如果你正在努力,可能比文档更有帮助:http://whoisnicoleharris.com/2015/01/06/implementing-django-formsets.html
It goes through using formsets which allow you to have and use more than one form of the same type on a page. And also shows you a JQuery plugin that allows you to add and remove forms dynamically.
它通过使用formsets,允许您在页面上拥有和使用多个相同类型的表单。并且还向您展示了一个JQuery插件,允许您动态添加和删除表单。