如何在Django模型的模型中实现可选的多个相同字段?

时间:2018-07-16 19:25:11

标签: django django-models django-forms django-views

我在Django中有以下模型:

class Profile(models.Model):
name = models.CharField(max_length=100)
address = models.ManyToManyField(Address, symmetric=False)

def __unicode__(self):
    return self.name

class Address(models.Model):
address= models.TextField()

def __unicode__(self):
    return self.address

和模型形式:

class ProfileForm(ModelForm):
class Meta:
    model = Profile
    fields='__all__'

个人资料可以有多个地址,即用户应该能够以一种形式添加多个地址。我该如何实现?

name: _______________
address: ______________
address: ______________
address: ______________

1 个答案:

答案 0 :(得分:1)

您需要表单集。这是django docs的解释,还有一个不错的tutorial