我有三个django模型(user
,member (extended user)
,subscription
),并且使用ForeignKey连接。
然后,当用户想要创建一个新帐户时,我将其保存到数据库中,如下所示:
language = Language.objects.get(key="EN")
country = Country.objects.get(key="BE")
user = User()
user.username = form.cleaned_data['email']
user.first_name = form.cleaned_data['first_name']
user.last_name = form.cleaned_data['last_name']
user.email = form.cleaned_data['email']
user.set_password(form.cleaned_data['password'])
user.save()
member = Member()
member.number = form.cleaned_data['member_id']
member.name = '{} {}'.format(form.cleaned_data['firstname'], form.cleaned_data['lastname'])
member.address = form.cleaned_data['address']
member.postcode = form.cleaned_data['postcode']
member.city = form.cleaned_data['city']
member.country = country
member.telephone = form.cleaned_data['telephone']
member.mobile = form.cleaned_data['mobile']
member.user = user
member.language = language
member.active = False
member.save()
subscription = Subscription()
subscription.started = datetime.date.today()
subscription.type = Type.objects.get(default=True)
subscription.member = member
subscription.save()
工作正常。
但是我想问一下是否有更好的方法将它存储到数据库中?